পিএইচপি লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান
পিএইচপি লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ৪ [FILES array দিয়ে ফাইল আপলোড করে ওয়াটারমার্ক করা]

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ৪ [FILES array দিয়ে ফাইল আপলোড করে ওয়াটারমার্ক করা]
আসসালামু আলাইকুম,
এরই মধ্যে দুটি পদ্ধতি দেখিয়ে ফেলেছি। সেগুলো এখানে পাবেন।
  1. পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ১
  2. পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ২
  3. পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ৩
এটাই হয়ত আমার পক্ষ থেকে পিএইচপি ইমেজ ওয়াটারমার্কিং এর শেষ পর্ব। তবে জিআর+ থেকে নয়। এরপরেও হয়তো আরও ভালো স্ক্রিপ্ট আসবে এই সম্পর্কে।



এবার আসব আমার চার নাম্বার স্ক্রিপ্টে।
এখানে আমরা ফাইল আপলোড করে ওয়াটার মার্ক বানাতে পারবো। এখানে ওয়াটার মার্ক ইমেজ আগে থেকে ঠিক করা থাকবে। কিন্তু মেইন ইমেজ ফাইল আমরা আপলোড করে নিব।
এখানে ফাইল এরে ব্যাবহার করে ইমেজ আপলোড করবো।


পিএইচপি প্রোগ্রাম রান করাতে হলে অবশ্যই ওয়েব সার্ভার লাগবে। কিন্তু ওয়েব সার্ভার থাকতে হলে লাগবে টাকা। টাকা যদি থাকে বা ওয়েবসারভার কেনা থাকলে এটা রান করা কোনো ব্যাপার না, ফাইল আপলোড করে ব্রাউজার দিয়ে খুললেই হবে।
আমাদের জন্য আমরা একটা অফলাইনে অ্যাপাচি সার্ভার সিমিউলেট করতে হবে। এর নাম xampp. 
ডাউনলোড করে নাও এখান থেকে https://www.apachefriends.org

এখন সেটআপ শেষ হলে সি ড্রাইভে গিয়ে xampp ফোল্ডারে যাও এবং htdocs ফাইল ফোল্ডার ওপেন কর।
সেখানে একটা ফাইল খুলো watermark নামে বা যেকোনো নামে।

এখন ব্রাউজার খুলো।

অ্যাড্রেস বারে লিখো http://localhost/"তোমার দেওয়া ফোল্ডারের নাম"
আমার ক্ষেত্রে water
http://localhost/water
এবার ঐ ফোল্ডারে index.php ফাইল তৈরি কর কোন টেক্সট ফাইল এডিটর দিয়ে।

এবার ঐ ডিরেক্টরিতে index.php তৈরি করে নিচের কোড লিখে সেভ কর,

<?php
if(isset($_FILES['image_file']))
{
$max_size = 800; //max image size in Pixels
$destination_folder = 'watermark';
$watermark_png_file = 'watermark.png'; //watermark png file
$image_name = $_FILES['image_file']['name']; //file name
$image_size = $_FILES['image_file']['size']; //file size
$image_temp = $_FILES['image_file']['tmp_name']; //file temp

$image_type = $_FILES['image_file']['type']; //file type
switch(strtolower($image_type)){ //determine uploaded image type
//Create new image from file
case 'image/png':
$image_resource =  imagecreatefrompng($image_temp);
break;
case 'image/gif':
$image_resource =  imagecreatefromgif($image_temp);
break;        
case 'image/jpeg': case 'image/pjpeg':
$image_resource = imagecreatefromjpeg($image_temp);
break;
default:
$image_resource = false;
}

if($image_resource){
//Copy and resize part of an image with resampling
list($img_width, $img_height) = getimagesize($image_temp);
    //Construct a proportional size of new image
$image_scale        = min($max_size / $img_width, $max_size / $img_height);
$new_image_width    = ceil($image_scale * $img_width);
$new_image_height   = ceil($image_scale * $img_height);
$new_canvas         = imagecreatetruecolor($new_image_width , $new_image_height);
if(imagecopyresampled($new_canvas, $image_resource , 0, 0, 0, 0, $new_image_width, $new_image_height, $img_width, $img_height))
{
if(!is_dir($destination_folder)){
mkdir($destination_folder);//create dir if it doesn't exist
}
//center watermark
$watermark_left = ($new_image_width/2)-(300/2); //watermark left
$watermark_bottom = ($new_image_height/2)-(100/2); //watermark bottom
$watermark = imagecreatefrompng($watermark_png_file); //watermark image
imagecopy($new_canvas, $watermark, $watermark_left, $watermark_bottom, 0, 0, 300, 100); //merge image
//output image direcly on the browser.
header('Content-Type: image/jpeg');
imagejpeg($new_canvas, NULL , 90);
//Or Save image to the folder
//imagejpeg($new_canvas, $destination_folder.'/'.$image_name , 90);
//free up memory
imagedestroy($new_canvas);
imagedestroy($image_resource);
die();
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#upload-form {
padding: 20px;
background: #F7F7F7;
border: 1px solid #CCC;
margin-left: auto;
margin-right: auto;
width: 400px;
}
#upload-form input[type=file] {
border: 1px solid #ddd;
padding: 4px;
}
#upload-form input[type=submit] {
height: 30px;
}
</style>
</head>
<body>
<form action="" id="upload-form" method="post" enctype="multipart/form-data">
<input type="file" name="image_file" />
<input type="submit" value="Send Image" />
</form>
</body>
</html>

হলুদ হাইলাইট করা অংশে ফাইলের সাইজ, ওয়াটার মার্ক ফাইল ঠিক করা হয়েছে।
কমলা হাইলাইট করা অংশে ইমেজের টেম্পরারি ফাইল বানান হয়েছে এবং ইমেজ সাইজ, ইমেজের এক্সটেনশন এবং ইমেজের নাম বের করা হয়েছে।
আকাশি হাইলাট অংশে ফাইলের এক্সটেনশন অনুযায়ী হেডার ফাইল সুইচ করা হয়েছে।
প্রতিটি অংশের পাশে কমেন্ট লেখা আছে, যাতে কোন অংশের কাজ কি বোঝা যায়। সেটা দেখলেই হবে। আমি আর তেমন কিছু লিখব না।

এবার index.php এর  সাথে একই ডিরেক্টরিতে watermark নামের ফোল্ডার বানাই ($destination_folder = 'watermark';  এর জন্য) এবং watermark.png রেখে দিয়ে ব্রাউজার দিয়ে চালু করি এভাবে।
http://localhost/water/index.php
আমি এখন FILES array দিয়ে করতে পারছি। মানে ফাইল আপলোড ইউজ করতে পারছি।
ধন্যবাদ সকলকে।

আমি ফেসবুকে আছি,

আরও পড়ুন

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ৩ [GET array দিয়ে ইমেজ এবং ওয়াটারমার্ক দুইটি ফাইলই ক্যাচ করা]

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ৩ [GET array দিয়ে ইমেজ এবং ওয়াটারমার্ক দুইটি ফাইলই ক্যাচ করা]
আসসালামু আলাইকুম,
এরই মধ্যে দুটি পদ্ধতি দেখিয়ে ফেলেছি। সেগুলো এখানে পাবেন।
  1. পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ১
  2. পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ২
এটাই হয়ত আমার পক্ষ থেকে পিএইচপি ইমেজ ওয়াটারমার্কিং এর শেষ পর্ব। তবে জিআর+ থেকে নয়। এরপরেও হয়তো আরও ভালো স্ক্রিপ্ট আসবে এই সম্পর্কে।

এবার আসব আমার তিন নাম্বার স্ক্রিপ্টে।
এখানে আমরা GET এরে দিয়ে ওয়াটারমার্ক ও মুল ইমেজ দুইটাই একসাথে ইনপুট দিতে পারবো।
মানে অনেকটা এভাবে
 localhost/water2/index.php?mainfile=image.jpg&watermark=logo.png
তো আমাদের প্রথমে একটা ফাইল আপলোড স্ক্রিপ্ট দিয়ে ফাইল আপলোড করে নিতে হবে। এখন আমি ফাইল আপলোড পিএইচপি স্ক্রিপ্ট নিয়ে আলোচনা করবো না।
তাই আমরা সরাসরি ডিরেক্টরিতে ইমেজ ফাইল রেখে নিবো।

পিএইচপি প্রোগ্রাম রান করাতে হলে অবশ্যই ওয়েব সার্ভার লাগবে। কিন্তু ওয়েব সার্ভার থাকতে হলে লাগবে টাকা। টাকা যদি থাকে বা ওয়েবসারভার কেনা থাকলে এটা রান করা কোনো ব্যাপার না, ফাইল আপলোড করে ব্রাউজার দিয়ে খুললেই হবে।
আমাদের জন্য আমরা একটা অফলাইনে অ্যাপাচি সার্ভার সিমিউলেট করতে হবে। এর নাম xampp. 
ডাউনলোড করে নাও এখান থেকে https://www.apachefriends.org

এখন সেটআপ শেষ হলে সি ড্রাইভে গিয়ে xampp ফোল্ডারে যাও এবং htdocs ফাইল ফোল্ডার ওপেন কর।
সেখানে একটা ফাইল খুলো watermark নামে বা যেকোনো নামে।

এখন ব্রাউজার খুলো।

অ্যাড্রেস বারে লিখো http://localhost/"তোমার দেওয়া ফোল্ডারের নাম"
আমার ক্ষেত্রে water2
http://localhost/water2
এবার ঐ ফোল্ডারে ওয়াটারমার্ক ডট  পিএইচপি ফাইল তৈরি কর কোন টেক্সট ফাইল এডিটর দিয়ে।




এবার ঐ ডিরেক্টরিতে index.php তৈরি করে নিচের কোড লিখে সেভ কর,

<?php

  // loads a png, jpeg or gif image from the given file name
  function imagecreatefromfile($image_path) {
    // retrieve the type of the provided image file
    list($width, $height, $image_type) = getimagesize($image_path);
    // select the appropriate imagecreatefrom* function based on the determined
    // image type
    switch ($image_type)
    {
      case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
      case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
      case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
      default: return ''; break;
    }

  }
  // load source image to memory
  $image = imagecreatefromfile($_GET['image']);
  if (!$image) die('Unable to open image');
  // load watermark to memory
  $watermark = imagecreatefromfile($_GET['watermark']);
  if (!$image) die('Unable to open watermark');

  // calculate the position of the watermark in the output image (the
  // watermark shall be placed in the lower right corner)
  $watermark_pos_x = imagesx($image) - imagesx($watermark) - 8;
  $watermark_pos_y = imagesy($image) - imagesy($watermark) - 10;
  // merge the source image and the watermark
  imagecopy($image, $watermark,  $watermark_pos_x, $watermark_pos_y, 0, 0,
    imagesx($watermark), imagesy($watermark));
  // output watermarked image to browser
  header('Content-Type: image/jpeg');
  imagejpeg($image, NULL, 100);  // use best image quality (100)
  // remove the images from memory
  imagedestroy($image);
  imagedestroy($watermark);
?> 

হলুদ হাইলাইট করা 100 হল ইমেজের অপাসিটি। মানে রঙের গাঢ়ত্ব। মানে ওয়াটারমার্ক ইমেজ কতো গাড় হবে সেটাই এটা ঠিক করে। এখানে ১০০ মানে ১০০%
লাল কালি করা অংশগুলো ইমেজের ফরম্যাট চেক করে এবং সেই অনুজায়ি হেডার ফাইল সুইচ করে বা বদলে দেয়।

এবার index.php এর  সাথে একই ডিরেক্টরিতে logo.png এবং কাঙ্ক্ষিত সকল ছবি রেখে দিয়ে ব্রাউজার দিয়ে চালু করি এভাবে।
http://localhost/water2/index.php?image=Penguins.png&watermark=logo.png


আমি এখন GET দিয়ে করতে পারছি।কিন্ত FILES দিয়ে করতে পারছি না। FILES array দিয়ে করতে পারলে ইমেজ আপলোড করে ওয়াটারমার্ক করা যেতো।এজন্যে এখন যেসব ছবি ওয়াটার মার্ক করবে, সেগুলো index.php এর সাথে একই ফোল্ডারে রাখতে হবে। ফাইল আপলোড অপশন অ্যাড করা গেলে আমি আবার পোস্ট করে জানিয়ে দেব।কেউ পারলে অবশ্যই আমাকে জানাবে এবং ইনবক্স করবে।

আমি ফেসবুকে আছি,


আরও পড়ুন

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ২ [ফাংশান ফাইল আলাদা করে ওয়াটারমার্ক তৈরি করা]

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ২ [ফাংশান ফাইল আলাদা করে ওয়াটারমার্ক তৈরি করা]
আসসালামু আলাইকুম,
এর আগের পোস্টে দেখিয়ে দিয়েছিলাম এক পদ্দতিতে। এবার আমরা আরেক পদ্দতিতে করবো।



পিএইচপি প্রোগ্রাম রান করাতে হলে অবশ্যই ওয়েব সার্ভার লাগবে। কিন্তু ওয়েব সার্ভার থাকতে হলে লাগবে টাকা। টাকা যদি থাকে বা ওয়েবসারভার কেনা থাকলে এটা রান করা কোনো ব্যাপার না, ফাইল আপলোড করে ব্রাউজার দিয়ে খুললেই হবে।
আমাদের জন্য আমরা একটা অফলাইনে অ্যাপাচি সার্ভার সিমিউলেট করতে হবে। এর নাম xampp. 
ডাউনলোড করে নাও এখান থেকে https://www.apachefriends.org

এখন সেটআপ শেষ হলে সি ড্রাইভে গিয়ে xampp ফোল্ডারে যাও এবং htdocs ফাইল ফোল্ডার ওপেন কর।
সেখানে একটা ফাইল খুলো watermark নামে বা যেকোনো নামে।

এখন ব্রাউজার খুলো।

অ্যাড্রেস বারে লিখো http://localhost/"তোমার দেওয়া ফোল্ডারের নাম"
আমার ক্ষেত্রে water2
http://localhost/water2
এবার ঐ ফোল্ডারে ওয়াটারমার্ক ডট  পিএইচপি ফাইল তৈরি কর কোন টেক্সট ফাইল এডিটর দিয়ে।

watermark.php ফাইলে নিচের লিঙ্কের কোড দিয়ে দাও।

<?php

class Watermark {
var $image;
var $type;
var $width;
var $height;
var $marked_image;
var $sizes;
var $position = "C";
var $offset_x;
var $offset_y;
var $orientation;
var $imageCreated = false;
var $gd_version;
var $fixedColor = '';

var $version = 'phpWatermark 0.3';

// Public Watermark
// You need to specify either a filename or an image resource
// when instatiating phpWatermark
function Watermark($res) {
list($this->type, $this->image) = $this->_getImage($res);

if (!$this->image) {
$this->_die_error("Your current PHP setup does not support ". $this->type ." images");
}

$this->width = imagesx($this->image);
$this->height = imagesy($this->image);

$gdinfo = gd_info();
if (preg_match('/(\d)\.\d/', $gdinfo["GD Version"], $gdinfo))
$this->gd_version = $gdinfo[1];
else
$this->gd_version = 0;
unset($gdinfo);
}

// Public void setType(string)
// Currently not used
// TODO: Add functionality
function setType($type) {
$this->type = $type;
}

// Public void addWatermark(string)
// Adds a watermark to the image
// Type defaults to TEXT for backwards compatibility
function addWatermark($mark, $type = "TEXT") {
//TODO: Support for watermark images (e.g. company logo)
//TODO: Automatically determine type of watermark

if ($type == "TEXT") { // We are going to embed text into the image
$this->orientation = ($this->width > $this->height) ? "H" : "V"; // Choose orientation

$this->sizes = $this->_getTextSizes($mark);

$this->_getOffsets();

// Copy a chunk of the original image (this is where the watermark will be placed)
$chunk = $this->_getChunk();
if (!$chunk) $this->_die_error("Could not extract chunk from image");

$img_mark = $this->_createEmptyWatermark();
$img_mark = $this->_addTextWatermark($mark, $img_mark, $chunk);

// Delete chunk
imagedestroy($chunk);

// Finish image
$this->_createMarkedImage($img_mark, $type, 30);
} elseif ($type == "IMAGE") { // We are going to embed an image
list($dummy, $mark) = $this->_getImage($mark);
$this->sizes = $this->_getImageSizes($mark);

$this->_getOffsets();

$img_mark = $this->_createEmptyWatermark();
$img_mark = $this->_addImageWatermark($mark, $img_mark);

$this->_createMarkedImage($img_mark, $type, 30);
}
}

// Public int getMarkedImage
// Returns the final image
function getMarkedImage() {
if ($this->imageCreated == false) {
$this->addWatermark($this->version);
}
return $this->marked_image;
}

// Public bool setPosition
// Set position of watermark on image
// Return true on valid parameter, otherwise false
function setPosition($newposition) {
$valid_positions = array(
"TL", "TM", "TR", "CL", "C", "CR", "BL", "BM", "BR", "RND"
);

$newposition = strtoupper($newposition);

if (in_array($newposition, $valid_positions)) {
if ($newposition == "RND") {
$newposition = $valid_positions[rand(0, sizeof($valid_positions) - 2)];
}
$this->position = $newposition;
return true;
}
return false;
}

// Public string setFixedColor
// Set a fixed color for text watermarks
// Return true on valid parameter, otherwise false
function setFixedColor($color) {
$text_color = array();
if (is_array($color) and sizeof($color) == 3) {
$text_color["r"] = $color[0];
$text_color["g"] = $color[1];
$text_color["b"] = $color[2];
} elseif (preg_match('/^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i', $color, $matches)) {
$text_color["r"] = hexdec($matches[1]);
$text_color["g"] = hexdec($matches[2]);
$text_color["b"] = hexdec($matches[3]);
} else {
return false;
}
foreach (array("r", "g", "b") as $key) {
if (!array_key_exists($key, $text_color) or $text_color[$key] < 0 or $text_color[$key] > 255) {
return false;
}
}
$this->fixedColor = $text_color;
return true;
}

// Private _die_error
function _die_error($errmsg) {
die($errmsg);
}

// Private _getTextSizes
function _getTextSizes($text) {
$act_scale = 0;
$act_font = 0;

$marklength = strlen($text);
$scale = ($this->orientation == "H") ? $this->width : $this->height; // Define maximum length of complete mark
$char_widthmax = intval(($scale / $marklength) - 0.5); // Maximum character length in watermark

for ($size = 5; $size >= 1; $size--) {
$box_w = imagefontwidth($size);
$box_h = imagefontheight($size);
$box_spacer_w = 0;
$box_spacer_h = 0;

if ($this->orientation == "H") {
$box_h *= 2;
$box_w *= 1.75;
$box_w *= $marklength;
$box_w += intval($this->width * 0.05);
$box_spacer_w = intval($this->width * 0.05);
$box_spacer_h = intval($this->height * 0.01);
} else {
$box_w *= 3;
$box_h *= 1.1;
$box_h *= $marklength;
$box_spacer_h = intval($this->height * 0.05);
$box_spacer_w = intval($this->width * 0.01);
}

$box_scale = ($this->orientation == "H") ? $box_w + $box_spacer_w : $box_h + $box_spacer_h;

if ($box_scale < $scale && $box_scale > $act_scale) { $act_font = $size; $act_scale = $box_scale; }
}

return array( "fontsize" => $act_font,
"box_w" => $box_w,
"box_h" => $box_h,
"spacer_w" => $box_spacer_w,
"spacer_h" => $box_spacer_h
);
}

// Private _getImageSizes
function _getImageSizes($res) {
// Check if the overlay image is bigger than the main image

if (@imagesx($res) > $this->width || @imagesy($res) > $this->height) {
// Need to resize the overlay image
$box_h = $box_w = 0;
$box_spacer_h = $box_spacer_w = 0;
if (imagesx($res) > imagesy($res)) {
$box_w = $this->width;
$box_h = intval((imagesy($res) / (imagesx($res) / $this->width)) + 0.5);
$box_spacer_h = intval(($this->height - $box_h) / 2);
} else {
$box_h = $this->height;
$box_w = intval((imagesx($res) / (imagesy($res) / $this->height)) + 0.5);
$box_spacer_w = intval(($this->width - $box_w) / 2);
}
} else {
$box_spacer_h = $box_spacer_w = 0;
$box_h = imagesy($res);
$box_w = imagesx($res);
}
return array(
"box_w" => $box_w,
"box_h" => $box_h,
"spacer_w" => $box_spacer_w,
"spacer_h" => $box_spacer_h
);
}


// Private _getChunk
function _getChunk() {
$chunk = imagecreatetruecolor($this->sizes["box_w"], $this->sizes["box_h"]);
#$chunk = imagecreate($this->sizes["box_w"], $this->sizes["box_h"]);
imagecopy( $chunk,
$this->image,
0,
0,
$this->offset_x,
$this->offset_y,
$this->sizes["box_w"],
$this->sizes["box_h"]
);
return $chunk;
}

// Private _createEmptyWatermark
function _createEmptyWatermark() {
return imagecreatetruecolor($this->sizes["box_w"], $this->sizes["box_h"]);
#return imagecreate($this->sizes["box_w"], $this->sizes["box_h"]);
}

// Private _addTextWatermark
function _addTextWatermark($mark, $img_mark, $chunk) {
imagetruecolortopalette($chunk, true, 65535);
$text_color = array("r" => 0, "g" => 0, "b" => 0);

if (is_array($this->fixedColor)) {
$text_color = $this->fixedColor;
} else {
// Search color for overlay text
for($x = 0; $x <= $this->sizes["box_w"]; $x++) {
for ($y = 0; $y <= $this->sizes["box_h"]; $y++) {
$colors = imagecolorsforindex($chunk, imagecolorat($chunk, $x, $y));
$text_color["r"] += $colors["red"];
$text_color["r"] /= 2;
$text_color["g"] += $colors["green"];
$text_color["g"] /= 2;
$text_color["b"] += $colors["blue"];
$text_color["b"] /= 2;
}
}
$text_color["r"] = $text_color["r"] < 128 ? $text_color["r"] + 128 : $text_color["r"] - 128;
$text_color["g"] = $text_color["g"] < 128 ? $text_color["g"] + 128 : $text_color["g"] - 128;
$text_color["r"] = $text_color["r"] < 128 ? $text_color["r"] + 128 : $text_color["r"] - 128;
}
// Choose transparent color for watermark
$mark_bg = imagecolorallocate( $img_mark,
($text_color["r"] > 128 ? 10 : 240),
($text_color["g"] > 128 ? 10 : 240),
($text_color["b"] > 128 ? 10 : 240));

// Choose text color for watermark
$mark_col = imagecolorallocate($img_mark, $text_color["r"], $text_color["g"], $text_color["b"]);

// Fill watermark with transparent color
imagefill($img_mark, 0, 0, $mark_bg);
imagecolortransparent($img_mark, $mark_bg);

// Add text to watermark
if ($this->orientation == "H") {
imagestring($img_mark, $this->sizes["fontsize"], 1, 0, $mark, $mark_col);
} else {
imagestringup($img_mark, $this->sizes["fontsize"], 0, $this->sizes["box_h"] - 5, $mark, $mark_col);
}

return $img_mark;
}

// Private _addImageWatermark
function _addImageWatermark($mark, $img_mark) {
$transparent_color_idx = imagecolortransparent($mark);
if ($transparent_color_idx >= 0) $transparent_color = imagecolorsforindex($mark, imagecolortransparent($mark));
imagecopy($img_mark, $mark, 0, 0, 0, 0, imagesx($mark), imagesy($mark));
if ($transparent_color_idx >= 0) {
$trans;
if (function_exists("imagecolorallocatealpha")) {
$trans = imagecolorallocatealpha(
$img_mark,
$transparent_color["red"],
$transparent_color["green"],
$transparent_color["blue"],
127
);
} else {
$trans = imagecolorallocate(
$img_mark,
$transparent_color["red"],
$transparent_color["green"],
$transparent_color["blue"]
);
}
imagecolortransparent($img_mark, $trans);
}

return $img_mark;
}

// Private _createMarkedImage
function _createMarkedImage($img_mark, $type, $pct) {
// Create marked image (original + watermark)
$this->marked_image = imagecreatetruecolor($this->width, $this->height);
imagecopy($this->marked_image, $this->image, 0, 0, 0, 0, $this->width, $this->height);
if ($type == 'TEXT') {
imagecopymerge(
$this->marked_image,
$img_mark,
$this->offset_x,
$this->offset_y,
0,
0,
$this->sizes["box_w"],
$this->sizes["box_h"],
$pct
);
$this->imageCreated = true;
} elseif ($type == 'IMAGE') {
if ($this->gd_version >= 2) { // GD2: Should be the easy way
imagealphablending($this->marked_image, true);

imagecopy(
$this->marked_image,
$img_mark,
$this->offset_x,
$this->offset_y,
0,
0,
$this->sizes["box_w"],
$this->sizes["box_h"]
);
} else {
imagecopymerge(
$this->marked_image,
$img_mark,
$this->offset_x,
$this->offset_y,
0,
0,
$this->sizes["box_w"],
$this->sizes["box_h"],
$pct
);
}

$this->imageCreated = true;
}
}

// Private _getOffsets
function _getOffsets() {

$width_mark = $this->sizes["box_w"] + $this->sizes["spacer_w"];
$height_mark = $this->sizes["box_h"] + $this->sizes["spacer_h"];
$width_left = $this->width - $width_mark;
$height_left = $this->height - $height_mark;

switch ($this->position) {
case "TL": // Top Left
$this->offset_x = $width_left >= 5 ? 5 : $width_left;
$this->offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "TM": // Top middle
$this->offset_x = intval(($this->width - $width_mark) / 2);
$this->offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "TR": // Top right
$this->offset_x = $this->width - $width_mark;
$this->offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "CL": // Center left
$this->offset_x = $width_left >= 5 ? 5 : $width_left;
$this->offset_y = intval(($this->height - $height_mark) / 2);
break;
default:
case "C": // Center (the default)
$this->offset_x = intval(($this->width - $width_mark) / 2);
$this->offset_y = intval(($this->height - $height_mark) / 2);
break;
case "CR": // Center right
$this->offset_x = $this->width - $width_mark;
$this->offset_y = intval(($this->height - $height_mark) / 2);
break;
case "BL": // Bottom left
$this->offset_x = $width_left >= 5 ? 5 : $width_left;
$this->offset_y = $this->height - $height_mark;
break;
case "BM": // Bottom middle
$this->offset_x = intval(($this->width - $width_mark) / 2);
$this->offset_y = $this->height - $height_mark;
break;
case "BR": // Bottom right
$this->offset_x = $this->width - $width_mark;
$this->offset_y = $this->height - $height_mark;
break;
}
}

// private _getImage
// Takes a path to an image or a php image resource as the only argument
// Returns image type and the appropriate image resource
function _getImage($res) {
$img;
$type;

if (intval(@imagesx($res)) > 0) {
$img = $res;
} else {
$imginfo = getimagesize($res);

switch($imginfo[2]) { // Determine type
case 1:
$type = "GIF";
if (function_exists("imagecreatefromgif")) {
$img = imagecreatefromgif($res);
} else {
die("Unsupported image type: $type");
}
break;
case 2:
$type = "JPG";
if (function_exists("imagecreatefromjpeg")) {
$img = imagecreatefromjpeg($res);
} else {
die("Unsupported image type: $type");
}
break;
case 3:
$type = "PNG";
if (function_exists("imagecreatefrompng")) {
$img = imagecreatefrompng($res);
} else {
die("Unsupported image type: $type");
}
break;
}
}

return array($type, $img);
}

}


?>


এই কোডেই কাজ শেষ না। এটা হল ফাংশন ফাইল।এখন আউটপুট ফাইল তৈরি করতে হবে।

এবার নতুন পিএইচপি ফাইল খুলো ইনডেক্স নামে। মানে index.php

এবার এই ইনডেক্স ফাইলে নিচের কোড লিখ,

<?php
// Load function
require("watermark.php");

// Instantiate phpWatermark
// The only parameter currently required is the name
// of the image, which should get marked
$wm = new watermark($_GET["file"]);

// Optionally specify the position of
// the watermark on the image
$wm->setPosition("RND");

// Add a watermark containing the string
// "phpWatermark" to the image specified above
$wm->addWatermark("logo.png", "IMAGE");

// Fetch the marked image
$im = $wm->getMarkedImage();

// Output
header("Content-type: image/png");
imagepng($im);

?>


লাল রঙ দিয়ে মার্ক করা watermark.php ফাইলটি ফাংশান ফাইল এখানে জয়েন করা হয়েছে। ওটা দিয়েই ইমেজের মারজিং এবং ভেরিয়েবল নেওয়া হবে। আর কমলা রঙের logo.png একই ডিরেক্টরিতে থাকবে যেটা মুল ছবির উপরে ওয়াটারমার্ক হিসেবে বসবে।

setPosition("RND") দিয়ে কো অরডিনেট কল করেছি। কো অরডিনেট গুলো ফাংশান ফাইলে আগে থেকেই আমি ভেরিয়েবল হিসেবে দিয়ে রেখেছি।সেখানে আরএনডি (RND) দিয়ে আমি র‍্যান্ডম কল করেছি। এতে একেকবার একেক ওয়াটারমার্ক কো অরডিনেট করবে।
নিচে দেখো, (এই কোড কোথাও পেস্ট করা লাগবে না)

switch ($this->position) {
case "TL": // Top Left
$this->offset_x = $width_left >= 5 ? 5 : $width_left;
$this->offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "TM": // Top middle
$this->offset_x = intval(($this->width - $width_mark) / 2);
$this->offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "TR": // Top right
$this->offset_x = $this->width - $width_mark;
$this->offset_y = $height_left >= 5 ? 5 : $height_left;
break;
case "CL": // Center left
$this->offset_x = $width_left >= 5 ? 5 : $width_left;
$this->offset_y = intval(($this->height - $height_mark) / 2);
break;
default:
case "C": // Center (the default)
$this->offset_x = intval(($this->width - $width_mark) / 2);
$this->offset_y = intval(($this->height - $height_mark) / 2);
break;
case "CR": // Center right
$this->offset_x = $this->width - $width_mark;
$this->offset_y = intval(($this->height - $height_mark) / 2);
break;
case "BL": // Bottom left
$this->offset_x = $width_left >= 5 ? 5 : $width_left;
$this->offset_y = $this->height - $height_mark;
break;
case "BM": // Bottom middle
$this->offset_x = intval(($this->width - $width_mark) / 2);
$this->offset_y = $this->height - $height_mark;
break;
case "BR": // Bottom right
$this->offset_x = $this->width - $width_mark;
$this->offset_y = $this->height - $height_mark;
break;
}
}
এখানে "TL", "TM", "TR", "CL", "C", "CR", "BL", "BM", "BR", "RND"
১০টা কো অরডিনেট সুইচ করবে। তুমি ইনডেক্সের setPosition("RND") এর ভেরিএবল বদলে বাকি ৯টির দিলে তারা সেই মাফিক প্রিন্ট করবে। তুমি চাইলে ফাংশান ফাইল থেকে কো অরডিনেট পজিশন বদলে দিতে পারবে।
(এই কোড কোথাও পেস্ট করা লাগবে না)


এবার ওয়াটার মার্ক ফাইল, যে ফাইল ওয়াটার মার্ক করবে সেটা সব ঠিক মতো ডিরেক্টরিতে রেখে এভাবে ব্রাউজার দিয়ে চালু করো,
http://localhost/water2/index.php?file=Penguins.png



আমি এখন GET দিয়ে করতে পারছি।কিন্ত FILES দিয়ে করতে পারছি না। FILES array দিয়ে করতে পারলে ইমেজ আপলোড করে ওয়াটারমার্ক করা যেতো।এজন্যে এখন যেসব ছবি ওয়াটার মার্ক করবে, সেগুলো index.php এর সাথে একই ফোল্ডারে রাখতে হবে। ফাইল আপলোড অপশন অ্যাড করা গেলে আমি আবার পোস্ট করে জানিয়ে দেব।কেউ পারলে অবশ্যই আমাকে জানাবে এবং ইনবক্স করবে।

আমি ফেসবুকে আছি,




আরও পড়ুন

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ১ [GET array দিয়ে ইমেজ ফাইল ক্যাচ করা]

পিএইচপি দিয়ে সহজে বানাও ওয়াটারমার্ক - ১ [GET array দিয়ে ইমেজ ফাইল ক্যাচ করা]
আসসালামু আলাইকুম,
এই বিষয়ে মুলত কমল ভাইজান এক্সপার্ট। তবুও আমার কাছে যে স্ক্রিপ্ট গুলো আছে তা আমি শেয়ার করবো ইনশাআল্লাহ।




পিএইচপি প্রোগ্রাম রান করাতে হলে অবশ্যই ওয়েব সার্ভার লাগবে। কিন্তু ওয়েব সার্ভার থাকতে হলে লাগবে টাকা। টাকা যদি থাকে বা ওয়েবসারভার কেনা থাকলে এটা রান করা কোনো ব্যাপার না, ফাইল আপলোড করে ব্রাউজার দিয়ে খুললেই হবে।
আমাদের জন্য আমরা একটা অফলাইনে অ্যাপাচি সার্ভার সিমিউলেট করতে হবে। এর নাম xampp. 
ডাউনলোড করে নাও এখান থেকে https://www.apachefriends.org

এখন সেটআপ শেষ হলে সি ড্রাইভে গিয়ে xampp ফোল্ডারে যাও এবং htdocs ফাইল ফোল্ডার ওপেন কর।
সেখানে একটা ফাইল খুলো watermark নামে বা যেকোনো নামে।

এখন ব্রাউজার খুলো।

অ্যাড্রেস বারে লিখো http://localhost/"তোমার দেওয়া ফোল্ডারের নাম"
আমার ক্ষেত্রে water
http://localhost/water
এবার ঐ ফোল্ডারে ইনডেক্স ডট  পিএইচপি ফাইল তৈরি কর কোন টেক্সট ফাইল এডিটর দিয়ে।

ইনডেক্স ফাইলে নিচের কোড দিয়ে দাও। (ভুল কোড ঠিক করা হয়েছে)

<?php
header('content-type: image/png');
if (isset($_GET['file'])) {
    $source = $_GET['file'];
    $watermark = imagecreatefrompng('logo.png');
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    $image = imagecreatetruecolor($watermark_width, $watermark_height);
    $image = imagecreatefrompng($source);
    $image_size = getimagesize($source);
    $x = $image_size[0] - $watermark_width - 10;
    $y = $image_size[1] - $watermark_height - 10;
    imagecopymerge ($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 20);
    imagepng($image);
}
else {
    echo 'No Source';
}
?>


লাল রঙ দিয়ে মার্ক করা logo.png ফাইলটি হবে ওয়াটারমার্ক ইমেজ। যেটা মুল ছবির উপরে বসবে।
সবুজ রঙ্গে মার্ক করা ভেরিয়েবল গুলো হল ওয়াটারমার্ক কো অরডিনেট। এর মাধ্যমে ওয়াটারমারকের অবস্থান বদলানো যায়।
আর কালো রঙ দিয়ে মার্ক করা ডিজিট হল ওয়াটার মার্ক কতটা গাড় হবে তার ভেরিয়েবল। ২০ থেকে যতো বাড়বে, ওয়াটার মার্ক আরও গাড় হবে। 

এবার ওয়াটার মার্ক ফাইল, যে ফাইল ওয়াটার মার্ক করবে সেটা সব ঠিক মতো ডিরেক্টরিতে রেখে এভাবে ব্রাউজার দিয়ে চালু করো,
http://localhost/water/index.php?file=Penguins.png
আমার এটা কেবল পিএনজি ফরম্যাটের ফাইল সাপোর্ট করে।
যদি অন্য ফাইল ফরম্যাট সাপোর্ট করাতে চাও তবে 
header('content-type: image/png'); বদলে
header('content-type: image/jpg'); বা
header('content-type: image/gif'); করে দাও

এবং imagecreatefrompng কে imagecreatefrom'format name' করে দাও।

এবং  imagepng($image); কে কাঙ্ক্ষিত ফাইল ফরম্যাট করে নিতে হবে।




আমি এখন GET দিয়ে করতে পারছি।কিন্ত FILES দিয়ে করতে পারছি না। FILES array দিয়ে করতে পারলে ইমেজ আপলোড করে ওয়াটারমার্ক করা যেতো।এজন্যে যেসব ছবি ওয়াটার মার্ক করবে, সেগুলো index.php এর সাথে একই ফোল্ডারে রাখতে হবে। ফাইল আপলোড অপশন অ্যাড করা গেলে আমি আবার পোস্ট করে জানিয়ে দেব।কেউ পারলে অবশ্যই আমাকে জানাবে এবং ইনবক্স করবে।

আমি ফেসবুকে আছি,





আরও পড়ুন

মোট পাতাদর্শিত