I'm creating thumbnails in PHP, but when I display the thumbnail on screen, the quality drastically drops. I'll post the php resample code:
$image_p = imagecreatetruecolor($newwidth, $newheight);
imagejpeg($image_p, null, 99);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $filewidth, $fileheight);
$imageformat($image_p, $_SERVER["DOCUMENT_ROOT"]."$filepath_thumb"); // Thumbnail Folder
How do I fix it. I've had people suggest tweaking the imagejpeg(); line. Any suggestions?
Related
I have to create dynamic student card image. add put this image object in student profile photo. But, student image color are changed.
How to put student profile photo with original color?
Here is my code:
header("Content-Type: image/jpeg");
$im = #imagecreate(602, 980)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$card_header = imagecreatefromjpeg('img/card/card-header.jpg');
imagecopy($im, $card_header, 0, 0, 0, 0, 602, 253);
$card_footer = imagecreatefromjpeg('img/card/card-footer.jpg');
imagecopy($im, $card_footer, 0, 834, 0, 0, 602, 146);
$student_photo = 'img/card/girls-profile.jpg'; //imagecreatefromjpeg($studentlist[0]->getCardPhoto());
// Get new sizes
list($width, $height) = getimagesize($student_photo);
$newwidth = 180;
$newheight = 220;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($student_photo);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopy($im, $thumb, 220, 220, 0, 0, $newwidth, $newheight);
imagejpeg($im, "uploads/card/test.jpeg");
imagedestroy($im);
Header Img:
Footer Img:
Profile Img:
Here is my output image:
Main problem is that your $im needs to be a true color image as well.
Secondly, you have to actually fill your background.
You can also skip creating $thumb and copyresized directly into your $im.
Heres a working verison (i changed your paths to test it on my machine)
<?php
header('Content-Type: image/jpeg');
$im = #imagecreatetruecolor(602, 980) // you want to create a truecolorimage here
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $background_color); // you have to actually use the allocated background color
$card_header = imagecreatefromjpeg('card-header.jpg');
imagecopy($im, $card_header, 0, 0, 0, 0, 602, 253);
$card_footer = imagecreatefromjpeg('card-footer.jpg');
imagecopy($im, $card_footer, 0, 834, 0, 0, 602, 146);
$student_photo = 'girls-profile.jpg';
// Get new sizes
list($width, $height) = getimagesize($student_photo);
$newwidth = 180;
$newheight = 220;
// Load
//$thumb = imagecreatetruecolor($newwidth, $newheight); // you can skip allocating extra memory for a intermediate thumb
$source = imagecreatefromjpeg($student_photo);
// Resize
imagecopyresized($im, $source, 220, 220, 0, 0, $newwidth, $newheight, $width, $height); // and copy the thumb directly
imagejpeg($im);
imagedestroy($im);
// you should also destroy the other images
imagedestroy($card_header);
imagedestroy($card_footer);
imagedestroy($source);
Keep in mind that your profile picture current gets distorted though, you may wan't to either make sure the profile pictures always have the correct aspect ratio or you may want to crop the image. See here for more details: PHP crop image to fix width and height without losing dimension ratio
Below is my image generate code:
$thumb = imagecreatetruecolor($newwidth, $newheight) or die('Cannot Initialize new GD image stream');
print_r($thumb);
$source = imagecreatefrompng($filename) or die('Cannot Initialize new GD image stream');
print_r($source);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
#imagepng( $thumb, $original_img_dir_absolute_path.$original_img_name, 0 );
it will generate first png but with black background only.
anybody have idea about it.
Your code seems to be OK. You may check if the fragment does not take a black part of the original image. You can also try to remove your "quality" parameter of imagepng() :
imagepng($thumb, $original_img_dir_absolute_path . $original_img_name);
Maybe your original image have a low contrast, and the low quality shows you a black image.
I have images with resolution of 720x1280 and I would need to store them on server with resolution 800x1500.
Since 720x1280 increased to height 1500 gives resolution of 844x1500 I would also need to crop image, remove 22 pixels from the left and right side.
For now I have this:
$img_url = file_get_contents($url);
$img = imagecreatefromstring($img_url);
$width = imagesx($img);
$height = imagesy($img);
$new_width = '800';
$new_height = '1500';
$thumb = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($thumb, $name, 100);
imagedestroy($thumb);
imagedestroy($img);
But the problem is that image is not cropped, 22 pixels from the left and right side are not removed.
Is there a way to do this, to first increase image resolution from url and then crop?
Googling php image crop reveals the secret:
$rect = [22, 0, 800, 1500]
$thumb = imagecrop($thumb, $rect)
In this line:
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
you just scale your original image to new dimensions.
As the documentation says:
If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed.
You need to set:
$shiftX = 22*(1280/1500); // consider passing variables rather than constant values
$scaledWidth = 720-$shiftX*2;
imagecopyresampled($thumb, $img, 0, 0, $shiftX, 0, $new_width, $new_height, $scaledWidth, $height);
which cuts out the required (proportional) rectangle of the source image to paste it in you destination picture.
i have this script to take original picture , resample it twice , for thumbnail and preview.This script works fine , even though You may find some weaknesses of syntactic fashion , im sure.The script as it is , is not subject of my question. I am wondering whether i am supposed to somehow clear memory afterwards.Am i flooding my server with data ? or is this fine and clears itself afterwards.Iam asking because this script will handle my gallery , and it is expected to handle multiple files at once.
script is written like this :
$filename = $DumpHere.$Processed;
// Get new dimensions
list($width, $height) = getimagesize($filename);
// Resample thumbnail
$image_p = imagecreatetruecolor(70, 70);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 70, 70, $width, $height);
// Output Thumbnail
imagejpeg($image_p, $ThumbsFolder.'thumb_'.$Processed, 100);
// Resample preview
$image_p = imagecreatetruecolor(500, 300);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 500, 300, $width, $height);
// Output Preview
imagejpeg($image_p, $PreviewFolder.'preview_'.$Processed, 100);
just to be clear
$DumpHere
is path to a folder containing original files before processing.Thanks for any help.
You want to use imagedestroy() on your resources, so just add:
imagedestroy($image_p);
imagedestroy($image);
At the end and that will free up the memory. PHP is pretty good about getting rid of memory on it's own. For example, once your script ends, all the memory is freed up. But this is the method to explicitly return those resources to the system.
This question already has answers here:
Transparent PNG over JPG in PHP
(2 answers)
Closed 9 years ago.
I want to do the following in PHP:
I have two images, a jpg and a png. I want to resize the jpg to the same size as the png then put the png on top. The PNG has transparency so I would like to preserve that so the jpg shows underneath.
If anyone could help that would be great!
Thanks
<?
$png = imagecreatefrompng('./mark.png');
$jpeg = imagecreatefromjpeg('./image.jpg');
list($width, $height) = getimagesize('./image.jpg');
list($newwidth, $newheight) = getimagesize('./mark.png');
$out = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($out, $jpeg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($out, $png, 0, 0, 0, 0, $newwidth, $newheight, $newwidth, $newheight);
imagejpeg($out, 'out.jpg', 100);
?>
This is the working code which i using
$dest = imagecreatefrompng('mapCanvas.png');
$src = imagecreatefromjpeg('si.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
// Copy and merge
imagecopymerge($dest, $src, 17, 13, 0, 0, 60, 100, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
Here is a link to an example that will overlay a transparent watermark onto an image. Might be your use case, might be related.
http://www.php.net/manual/en/image.examples.merged-watermark.php
There is also a way to load JPG images, resize images, turn on alpha tracking, and export images in GD.
Jacob