how to set x and y coordinates in image manipulation. - php

i just do image watermarking in php it's working but not setting image like i want,here's my code of php file.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];
$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname=$_FILES["fileToUpload"]["name"];
// Set the thumbnail name
$thumbnail = $folder.$newname."_thumbnail.jpg";
$actual = $folder.$newname.".jpg";
$imgname=$newname."_thumbnail.jpg";
// Load the mian image
$source = imagecreatefromjpeg($uploadimage);
// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');
// get the width and height of the watermark image
$water_width = imagesx($watermark);
$water_height = imagesy($watermark);
// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);
// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis
$dime_x = 0;
$dime_y = 0;
// copy both the images
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);
// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
}
?>
<img src='uploads/<?php echo $imgname;?>'>
</body>
</html>
and my html code also working fine.but problem with generated image it's like that
text with 'JACLIN ADMIN' is my png image and i want to apply it in middle from up and left. i just put 0 for both but problem is how can i put it in middle dynamicaly when size of images with diffrent height and width?please help me.

First, you need to find the middle point of your image:
$im_middle_w = $main_width/2;
$im_middle_h = $main_height/2;
Then you just need to add the watermark there but you need to move the watermark to the left by half (so it's actually centered):
$dime_x = $im_middle_w - $water_width/2;
$dime_y = $im_middle_h - $water_height/2;
Haven't tested it but it should work. If it doesn't work, feel free to link the images and I'll see to the code myself.

First set Value:
$watermark_pos_x = (imagesx($image)/2) - (imagesx($watermark)/2) - 15; $watermark_pos_y = (imagesy($image)/2) - (imagesy($watermark)/2) - 10;
Then after apply value on Function:
// merge the source image and the watermark
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, imagesx($watermark), imagesy($watermark));

Related

PHP imagecopymerge() - one image is stretched

I'm am trying to add a 50px white margin down the right side of an image by creating an empty image that's 50px wider than the source image and then merging the source image onto it. The problem is that the source image just gets stretched sideways so it's 50px wider!
Maybe I'm using the wrong function to merge the images ...
here's my code
$destImage = $filepath;
#echo "dest image = ".$destImage;
$sourceImage = imagecreatefrompng($filepath);
// dimensions
$src_wide = imagesx($sourceImage);
echo "src_wide=".$src_wide;
$src_high = imagesy($sourceImage);
// new image dimensions with right padding
$dst_wide = $src_wide+50;
echo "dst_wide=".$dst_wide;
$dst_high = $src_high;
// New resource image at new size
$dst = imagecreatetruecolor($dst_wide, $dst_high);
// set white padding color
$clear = array('red'=>255,'green'=>255,'blue'=>255);
// fill the image with the white padding color
$clear = imagecolorallocate( $dst, $clear["red"], $clear["green"], $clear["blue"]);
imagefill($dst, 0, 0, $clear);
// copy the original image on top of the new one
imagecopymerge($dst,$sourceImage,0,0,0,0,$src_wide,$src_high, 100);
imagepng($dst,$destImage,6);
imagedestroy($dst);
chmod($destImage,0775);
what am I doing wrong here ??
thanks
It's stretching because you are copying it to the full width of the destination image. Instead use
imagecopyresampled($dst,$sourceImage,50,0,0,0,$src_wide,$src_high,$src_wide,$src_high);

image manipulation width and height setting

in my project i just do image watermarking or image combine it's working fine and code for that.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];
$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname= time();
$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
// Set the thumbnail name
$thumbnail = $folder.$newname.".".$ext;
$imgname=$newname.".".$ext;
// Load the mian image
if ($ext=="png" || $ext=="PNG") {
$source = imagecreatefrompng($uploadimage);
}
else if ($ext=="gif" || $ext=="GIF") {
$source = imagecreatefromgif($uploadimage);
}
else if ($ext=="bmp" || $ext=="BMP") {
$source = imagecreatefrombmp($uploadimage);
}
else{
$source = imagecreatefromjpeg($uploadimage);
}
// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');
// get the width and height of the watermark image
$water_width = imagesx($source)/2;
$water_height = imagesy($watermark);
// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);
$im_middle_w = $main_width/2;
$im_middle_h = $main_height/2;
// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis
$dime_x = $im_middle_w - $water_width/2;
$dime_y = $im_middle_h - $water_height/2;
// copy both the images
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);
// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
unlink($file);
}
?>
<img src='uploads/<?php echo $imgname;?>'>
</body>
</html>
but problem with setting $water_width and i want set as half of my source image. but when i have source image of less width or more width compare to $water_width it's set it like that. see image when source image width is more.
and when width is less.
so my problem is how to set $water_width as half of source image width?
by Alex your answer it's came up like this.
This will resize watermark to half-width of original image and put it in the centre:
// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');
// get the width and height of the watermark image
$water_width = imagesx($watermark);
$water_height = imagesy($watermark);
// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);
// resize watermark to half-width of the image
$new_height = round($water_height * $main_width / $water_width / 2);
$new_width = round($main_width / 2);
$new_watermark = imagecreatetruecolor($new_width, $new_height);
// keep transparent background
imagealphablending( $new_watermark, false );
imagesavealpha( $new_watermark, true );
imagecopyresampled($new_watermark, $watermark, 0, 0, 0, 0, $new_width, $new_height, $water_width, $water_height);
// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis
$dime_x = round(($main_width - $new_width)/2);
$dime_y = round(($main_height - $new_height)/2);
// copy both the images
imagecopy($source, $new_watermark, $dime_x, $dime_y, 0, 0, $new_width, $new_height);
// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
imagedestroy($source);
imagedestroy($watermark);
imagedestroy($new_watermark);
You can try imagettftext method, if you don't want any such high perfection in transparency. You can have try of this code. You have to save a font file in your directory, here I used arial.ttf.
$im = imagecreatefrompng("png.png"); //create image data
$font = 'arial.ttf'; //font file name
$randomString = "example.com"; //string need to be shown
$main_width = imagesx($im); //finding width and height
$main_height = imagesy($im);
$posx= $main_width/2; //finding center
$posy = $main_height/2;
$color = imagecolorallocate($im, 200, 200, 200); //Creating color
$size = ($main_width/25)+1; //determine size of font. +1 to avoid 0
$temp = $size*5;
$posx = $posx-$temp; //adjust to average center
imagettftext($im,$size,0, $posx, $posy, $color, $font , $randomString); //apply a text
You have to adjust posx and posy for your text position. Size also can be adjusted with some logics.
$color = imagecolorallocate($im, 0, 0, 0);= black
and $color = imagecolorallocate($im, 255, 255, 255); = white.
You have to adjust this for your required text color.

php add watermark on a s3 imported image

I'm trying to add a watermark on an image.
Both , the image and the watermark image are imported from the aws s3.
I tried to do this
$watermark = tempnam(sys_get_temp_dir(), 'watermark.png');
//save the file from s3 storage to the temporary file
$content=$this->s3Manager->getFile('watermark.png',$this->verb,$watermark);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
list($watermark_width,$watermark_height) = getimagesize($watermark);
//Get the width and height of your image - we will use this to calculate where the watermark goes
$size = getimagesize($tempFile);
//Calculate where the watermark is positioned
//In this example, it is positioned in the lower right corner, 15px away from the bottom & right edges
$dest_x = $size[0] - $watermark_width - 15;
$dest_y = $size[1] - $watermark_height - 15;
imagecopy($tempFile, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
//Finalize the image:
imagejpeg($tempFile);
return $tempFile;
It's failing on the imagecopy method -
"imagecopy() expects parameter 1 to be resource, string given in.
I checked if the images a successfully imported by the dest_y and dest_x and they seems ok.
As the error says, imagecopy wants a resource. I assume tempfile is a string. You can do this
$res = imagecreatefrompng($tempfile)
and pass this to imagecopy

Crop Image with jWindowCrop - PHP

I'm using this jQuery plugin to crop images:
http://www.tmatthew.net/jwindowcrop
As you can see, it's really easy to use it on jQuery side, but my problem is with cropping the real image with PHP/GD.
with some goggling, I got:
$targ_w = $targ_h = 150;
$jpeg_quality = 90;
$src = 'demo_files/flowers.jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r, null, $jpeg_quality);
But it's not taking care of zoom ins/zoom outs made by the jQuery plugin, how I should crop the image and save it using this plugin and PHP?
I figured this out, here is my code in case of anyone else has the same question, the cropping will be done by Zebra image class:
http://stefangabos.ro/php-libraries/zebra-image/#documentation
PHP:
// The variables we got from the plugin in upload page:
$x = intval($_POST['x']);
$y = intval($_POST['y']);
$w = intval($_POST['w']);
$h = intval($_POST['h']);
// The img file which we want to crop
$tmp_file = 'path/to/img';
// Now include the Zebra class
include_once('path/to/Zebra_Image.php');
$image = new Zebra_Image();
$image -> preserve_aspect_ratio = true;
$image -> enlarge_smaller_images = true;
$image -> preserve_time = true;
$image -> jpeg_quality = 100;
// Now imagine that the user has selected the area which he want with the plugin, and we also want to make the image out put in a specific size(200*225):
$target_path = 'new/img/path'; // the output img path
$image -> source_path = $tmp_file;
$image -> target_path = $target_path;
$image -> crop($x, $y, $x + $w, $y + $h);
$image -> source_path = $target_path;
$image -> resize(200, 225, ZEBRA_IMAGE_CROP_CENTER);
I am also using jwindowcrop. When you click jwindowcrop's zoom, the w and h changes. (see attached picture)
You have to make sure you used the correct parameters as stated in the php manual
http://www.php.net/manual/en/function.imagecopyresampled.php
In my case, I used imagecopyresized and I could crop the image properly including the zooms
dst_image
Destination image link resource.
src_image
Source image link resource.
dst_x
x-coordinate of destination point.
(in my case the destination image should start from upper left corner)
dst_y
y-coordinate of destination point.
(in my case the destination image should start from upper left corner)
src_x
x-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from x=231, 231 pixels far from the left edge)
src_y
y-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from y=706, 706 pixels far from the top edge)
dst_w
Destination width.
(in my case, my new image should have a width of 800px)
dst_h
Destination height.
(in my case, my new image should have a height of 400px)
src_w
Source width.
(when my cropping function zooms, it changes the width and height of the original image)
src_h
Source height.
(when my cropping function zooms, it changes the width and height of the original image)
imagecopyresized(dst_image, src_image, 0, 0, 231, 706, 800, 400, 521, 318);

Photo color loss after watermarking

Having a few teething problems watermarking a photo. It all works fine apart from the watermarked photo's colors become duller than they should be - very noticeable in-fact.
I'm using imagecopyresized to do my watermarking, as this specifically allows me to use PNG-24 watermarks, the others do not. I know the colors are usually OK, as I have just used readfile($url) as a test, and the photos are perfect.
Here is my script:
<?php
// get parent and watermark images & sizes
$image = imagecreatefromjpeg($url);
$imageSize = getimagesize($url);
$watermark = imagecreatefrompng('watermark.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
// calculate new watermark width and position
if ($imageSize[0] > $imageSize[1] || $imageSize[0] == $imageSize[1]) {
$leftPercent = 23;
} else {
$leftPercent = 7;
}
$leftPixels = ($imageSize[0]/100)*$leftPercent;
$newWatermarkWidth = $imageSize[0]-$leftPixels;
$newWatermarkHeight = $watermark_o_height * ($newWatermarkWidth / $watermark_o_width);
// place watermark on parent image, centered and scaled
imagecopyresized(
$image,
$watermark,
$imageSize[0]/2 - $newWatermarkWidth/2,
$imageSize[1]/2 - $newWatermarkHeight/2,
0,
0,
$newWatermarkWidth,
$newWatermarkHeight,
imagesx($watermark),
imagesy($watermark)
);
// print
imagejpeg($image);
// destroy
imagedestroy($image);
imagedestroy($watermark);
?>
How can I stop this from happening? I'm reading about imagecreatetruecolor, does that solve the issue? I'm Googling "imagecreatetruecolor color loss photos" and variations but nobody really talks about this issue. If I do need this function, where would I add that to this script?
This has totally thrown a spanner in the works for me and would love for somebody to tell me where to stick it (not literally).
Here is an example of the color loss. The preview image should be exactly the same colors as the thumbnail. The thumbnails are created using readfile() whereas the previews are created using imagecreatefromjpeg and imagecopresized.
This example code works fine, by using the same characteristics as your images:
Original JPG: dark background; beautiful girl; red dress.
Watermark PNG: transparent background; text; gray color.
<?php
// Path the the requested file (clean up the value if needed)
$path = $url;
// Load image
$image = imagecreatefromjpeg($path);
$w = imagesx($image);
$h = imagesy($image);
// Load watermark
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
// Merge watermark upon the original image (center center)
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
// Output the image to the browser
header('Content-type: image/jpeg');
imagejpeg($image);
// destroy both images
imagedestroy($image);
imagedestroy($watermark);
// kill script
exit();
?>
Left: Output Image | Right: Original Image
Note:
The output image was compressed several times until: Original -> PHP Output -> GIMP -> Here.
After much testing, I came to the conclusion that PHP's GD Image does not support color profiles on the images that are being watermarked. I am now using Imagick and the colors are perfect.

Categories