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.
Related
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
i want to merge image with it own flipped image and rotated at angle say -60 degree.
Suppose i have image of 1000*1000 px then
A: original image resized to 500*500px and places in right bottom part of 1000*1000 square box
B:same image a flipped and rotated by -600 degree and place in top left corner of 1000*1000 square box
everything works fine but just issue like
1: some border for rotated images
2: background on rotate one is more darker make it look bid odd
test case :
sample image (thumbnail is resized, click to open original image) :
output image :
Below is my code
$image1=$image2=imagecreatefrompng('a.png');
//filter_opacity( $image1, 25 );
$w=imagesx($image1);
$h=imagesy($image1);
$finala = imagecreatetruecolor($w, $h);
$finalb = imagecreatetruecolor($w, $h);
$finalc = imagecreatetruecolor($w, $h);
$backgroundColora = imagecolorallocate($finala, 250,252,252); // gray
$backgroundColorb = imagecolorallocate($finalb, 250,250,250); // gray
$backgroundColorc = imagecolorallocate($finalc, 250,250,250); // gray
imagefill($finala, 0, 0, $backgroundColora);
imagefill($finalb, 0, 0, $backgroundColorb);
imagefill($finalc, 0, 0, $backgroundColorc);
$percent = 0.583;
$new_width = $w * $percent;
$new_height = $h * $percent;
$wshift = $w/8.5;
$hshift = $h/2.5;
imagecopy($finala, $image1, 0,0,0,0,$w,$h);
imagecopyresampled($finala, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
imagecopy($finalc, $image1, 0,0,0,0,$w,$h);
imagecopyresampled($finalc, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
imagecopyresampled($finalb, $finalc,$w*0.3,$h*0.3,0,0, $w*0.6, $h*0.6, $w, $h);
imageflip($finala, IMG_FLIP_HORIZONTAL );
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 0, 0, 0, 127));
imagecopyresampled($finalb, $finala,-$w*0.1,-$h*0.1,0,$h*0.20, $new_width, $new_height, $w, $h);
header('Content-Type: image/jpeg');
imagejpeg($finalb);
imagedestroy($finala);
imagedestroy($image2);
imagedestroy($image1);
imagedestroy($final);
As you need to rotate and place same image at top and bottom, you no need to pass same image twice with $_GET single time is enough
// Source image
$image = imagecreatefrompng("test.png");
$w=imagesx($image);
$h=imagesy($image);
$finalImage = imagecreatetruecolor($w, $h);
// Bg add
$backgroundColor = imagecolorallocate($finalImage, 252,252,252); // gray
imagefill($finalImage, 0, 0, $backgroundColor);
// New sizes calculation
$percent = 0.5;
$new_width = $w * $percent;
$new_height = $h * $percent;
// First image add
imagecopyresampled($finalImage, $image, $new_width, $new_height, 0, 0,
$new_width, $new_height, $w, $h);
// Second image rotate with transparant bg
$transparency = imagecolorallocatealpha( $image,0,0,0,127 );
$rotatedImage = imagerotate( $image, -60, $transparency, 1);
imagealphablending( $rotatedImage, false );
imagesavealpha( $rotatedImage, true );
// Getting new rotated image sizes to avoid cutting border
$rw = imagesx($rotatedImage);
$rh = imagesy($rotatedImage);
// Second rotated image add with bigger space
imagecopyresampled($finalImage, $rotatedImage, 0, 0, 0, 0,
$rw*$percent, $rh*$percent, $rw, $rh);
// Outputing png image
header( 'Content-Type: image/png' );
imagepng( $finalImage );
change this line:
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 0, 0, 0, 127));
to
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 250, 252, 252, 127));
the black board should gone!
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);
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);
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...