Extend canvas around image - php

I've got this code. My problem, that it doesn't work correctly. It extends canvas around image, but image "deformed". I don't know where is it the problem?!
$width = 100;
$height = 80;
//$source_width = 50;
//$source_height = 30;
if ($width > $source_width AND $height > $source_height)
{
$width_new = $width;
$height_new = $height;
$dst_x = ($width - $source_width)/2;
$dst_y = ($height - $source_height)/2;
}
$img = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
imagecopyresampled($img, $source_image, $dst_x, $dst_y, 0, 0, $width_new, $height_new, $source_width, $source_height);

Your destination height and width are larger than the source height and width. Make them the same.
You should only use the larger height and width for the canvas (the white)
For example:
imagecopyresampled($img, $source_image, $dst_x, $dst_y, 0, 0, $width_new, $height_new, $source_width, $source_height);
Should be:
imagecopyresampled($img, $source_image, $dst_x, $dst_y, 0, 0, $source_width, $source_height, $source_width, $source_height);

Related

Black color at cropped borders when using it in gdi, but looks transparent in photo editor

Borrowed this code to circle crop an image output.
// create the transparent circle image
$filename = APP_WEB_PATH."img/user/".$_GET['img'].".jpg";
$imagefilenamepng = APP_WEB_PATH."img/user/".$_GET['img']."_c.png";
$image_s = imagecreatefromstring(file_get_contents($filename));
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 300;
$newheight = 300;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);
imagepng($image,$imagefilenamepng);
imagedestroy($image);
imagedestroy($mask);
The image looks good and transparent and even when opened with a photo editor
but when used inside an image using php GDI i get a black border instead of transparent
what am I missing here.. (if I use paint.net image editor and re-save the image, then it works fine..) the below image works fine after saving in the photo editor
I use the code to call it as
self::$im = imagecreatefromjpeg($imgname1);
$im2 = imagecreatefrompng($imgname2);
$sx = imagesx($im2)*$scale;
$sy = imagesy($im2)*$scale;
$stamp = imagescale($im2, $sx);
imagecopy(self::$im, $stamp, $xcord, $ycord, 0, 0, $sx, $sy);
here self::$im is the larger image, and $im2 is the one that is cropped

center an image when resizing an image and before imagefill

I'm new to php, actually new to everything in general. How do i center an image before imagefill? Right now the image goes to the left if the ratio is smaller than the new ratio and everything on the right is filled in white.
I've tried imagecopyresampled($tn, $img_source, 0, 0, -$new_width/2, 0, $new_width, $new_height, $width, $height); well that just cuts everything in half of what the position is.
if ( $image_ratio <= $mod_ratio ){
$new_height = $mod_height;
$new_width = $width / ($height / $mod_height);
}
else {
$new_width = $mod_width;
$new_height = $height / ($width / $mod_width);
}
//blah blah
$tn= imagecreatetruecolor($mod_width, $mod_height);
$whiteBackground = imagecolorallocate($tn, 255, 255, 255);
imagefill($tn,0,0,$whiteBackground);
imagecopyresampled($tn, $img_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

positioning the watermark php GD

I want the watermark to go BELOW the image, so the overall height of image should expand.
Here is my code:
$img_width=imagesx($img);
$img_height=imagesy($img);
$watermark=imagecreatefrompng($watermark);
$watermark_width=imagesx($watermark);
$watermark_height=imagesy($watermark);
$image=imagecreatetruecolor($watermark_width, $watermark_height);
imagealphablending($image, false);
$dest_x=$img_width-$watermark_width-5;
$dest_y=$img_height-$watermark_height+1;
imagecopy($img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagesavealpha($img, true);
I tried playing with watermark_width and height no luck.
Is this even possible?
You need to create an image with dimensions:
imagecreatetruecolor(max($img_width,$watermark_width), $img_height + $watermark_height + $margin);
then, copy the image to (0,0)
last, copy the watermark to (0,$img_height + $margin)
($margin is the space between image & watermark in pixels)
Edit:
$margin = 5;
$img = imagecreatefrompng($img_path);
$watermark = imagecreatefrompng($watermark_path);
$img_width = imagesx($img);
$img_height = imagesy($img);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$output_width = max($img_width, $watermark_width);
$output_height = $img_height + $watermark_height + $margin;
$output = imagecreatetruecolor($output_width, $output_height);
imagesavealpha($output, true);
imagealphablending($output, false);
imagerectangle($output, 0, 0, $output_width, $output_height, imagecolorallocatealpha($output, 0, 0, 0, 127));
imagecopy($output, $img, 0, 0, 0, 0, $img_width, $img_height);
imagecopy($output, $watermark, 0, $img_height + $margin, 0, 0, $watermark_width, $watermark_height);
imagepng($output, $path_to_save); // use null to output
imagedestroy($img);
imagedestroy($output);
imagedestroy($watermark);

PHP GD transparency doesn't work with iPhone

I am using code to create an image with PHP GD that needs a fully transparent background. When I create it, it displays fine in a browser, but bad in my iPhone app. I don't know why, but in my iPhone it displays all transparency with black. This seems to be a GD problem because when I loaded my GD image into a web editor and reexported it, it displayed fine in my iPhone app. Is there a special way I should export the png image from GD or something or is this some sort of bug? Here is the code:
$filename = "./me.jpg";
$image_s = imagecreatefromjpeg($filename);
list($current_width, $current_height) = getimagesize($filename);
$left = isset($_GET['pl']) ? abs($_GET['pl']) : 0;
$top = isset($_GET['pt']) ? abs($_GET['pt']) : 0;
$width = isset($_GET['cs']) ? abs($_GET['cs']) : 65;
$height = isset($_GET['cs']) ? abs($_GET['cs']) : 65;
$canvas = imagecreatetruecolor($width, $height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
$newwidth = 65;
$newheight = 65;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $canvas, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 255, 255);
imagecolortransparent($mask, $transparent);
imagefilledellipse($mask, $newwidth / 2, $newheight / 2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth + 10, $newheight + 10, 100);
imagecolortransparent($image, $red);
imagefill($image,0,0, $red);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($mask);
Have you tried using imagesavealpha instead of imagesettransparency? Set alpha blending to false and then use imagesavealpha to true. Finally you'll call the imagecolorallocatealpha function to get your transparent/alpha color instead of imagesettransparency:
imagealphablending($image, false);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefilledellipse($mask, $newwidth / 2, $newheight / 2, $newwidth, $newheight, $transparent);
etc...

php gd, use two image, bad quality

i change image size and put on others, but image quality very poor, why?
(When i save image I set 100 quality)
$src = imagecreatetruecolor($new_width, $new_height);
$src2 = imagecreatefromjpeg($img_url);
imagecopyresampled($src, $src2, 0, 0, 0, 0, $new_width, $new_height, $new_img_size['org_w'], $new_img_size['org_h']);
$bg_size = 600;
$img_center_w = ($bg_size / 2) - ($new_width / 2);
$img_center_h = ($bg_size / 2) - ($new_height / 2);
$dst = imagecreate($bg_size, $bg_size );
$bg = imagecolorallocate($dst, 225, 255, 255);
imagecopy($dst, $src, $img_center_w, $img_center_h, 0, 0, $new_width, $new_height);
imagejpeg($dst, 'test_img.jpg', 100);
$dst = imagecreate($bg_size, $bg_size );
I guess that's the problem. You should use imagecreatetruecolor as above.

Categories