Resize image doesn't work with PHP-GD - php

Following my code:
list($width, $height) = getimagesize("dir");
$src = imagecreatefromjpeg("dir");
$im = imagecreatetruecolor(300, 300);
imagecopyresampled($im, $src, 0, 0, 0, 0, 300, 300, $width, $height);
imagejpeg($src, 'img.jpg');
It saves the image, but with the same size of originary image and not 300x300. How to solve?

You have used wrong image handler with imagejpeg function. Use:
imagejpeg($im, 'img.jpg');

Related

Image color changed with imagecreate in php

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

imagecopyresampled not working not sure why

So I'm attempting to use imagecopyresampled to crop out a section of a photo so that I don't have to worry about my users uploading photos larger than intended to my website. Unfortunately I have yet to figure out why imagecopyresampled is basically behaving as though I simply resized the image using CSS. From my understanding it should only copy a section of the image at 0,0 based on the coordinates I've provided to a 325X300 px jpg.
!:example
The top image is the one I'm using imagecopyresampled to generate. My code is as follows. Just trying to understand what I'm doing wrong here because apparently my copy of GD doesn't have imagecrop otherwise I'd probably be using that.
<html>
<style>
.sample{
width: 325;
height: 300;
}
</style>
<body>
<?php
$image = imagecreatefromjpeg('Image6.jpg');
$filename = 'Thumbnail_Image6.jpeg';
$width = 325;
$height = 300;
$oldwidth = imagesx($image);
$oldheight = imagesy($image);
if( $oldwidth > 325 || $oldheight > 300){
$thumb = ImageCreateTrueColor( 325, 300);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 325, 300, $oldwidth, $oldheight);
imagejpeg($thumb, $filename, 100);
echo "<img src='".$filename."'><br>";
echo "<img class='sample' src='Image6.jpg'><br>";
}
?>
</body>
</html>
if you're going to crop the image, you don't need to use full image size.
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 325, 300, 325, 300);
Mainly because your source size is the full size of the source image, so it's resizing it instead of taking a chunk out. Try this to see what I mean:
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $width, $height);
You can offset the chunk by changing the source x and y values. Example:
imagecopyresampled($thumb, $image, 0, 0, 50, 50, $width, $height, $width, $height);

upload converted imagejpeg to server

This is a function that creates a .jpg file from an uploaded .png file.
$input_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.png';
$output_file = 'img/uploaded/'.$_SESSION['userid'].'-'.$_SESSION['username'].'.jpg';
$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);
What I would like to know is how to define a custom "quality" or compression-rate for the new JPEG image. In some other conversion-functions there is a possibility to choose between 0(max compression) and 100(max quality). Does anybody know how to do this in my case?
The last parameter of imagejpeg() is "quality":
This should set it to max quality:
imagejpeg($output, $output_file, 100);
For more info. check this out:
http://php.net/manual/en/function.imagejpeg.php

Resizing images with GD without color loss

I'm trying to resize an image with GD and am seeing a color loss on the resized image. Here is my code:
$src = imagecreatefromstring(file_get_contents($source));
ImageCopyResized($dst, $src, 0, 0, 0, 0, $t_width, $t_height, ImageSX($src), ImageSY($src));
Imagejpeg($dst, $dest, 90);
Are you using imagecreatetruecolor when declaring $dst?
The right way to do this is:
$dst = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($dst, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

On-the-fly thumbnails PHP

I came up with this:
<?php
$dir = $_GET['dir'];
header('Content-type: image/jpeg');
$create = imagecreatetruecolor(150, 150);
$img = imagecreatefromjpeg($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, 150, 150);
imagejpeg($create, null, 100);
?>
It works by accessing:
http://example.com/image.php?dir=thisistheimage.jpg
Which works fine... but the output is awful:
Can someone fix my code for the image to be 150 x 150 covering the black area...
Thanks.
SOLUTION:
<?php
$dir = $_GET['dir'];
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($dir);
$create = imagecreatetruecolor(150, 150);
$img = imagecreatefromjpeg($dir);
$newwidth = 150;
$newheight = 150;
imagecopyresized($create, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($create, null, 100);
?>
Use imagecopyresized:
$newwidth = 150;
$newheight = 150;
imagecopyresized($create, $image, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
The last 2 150 should be the original width and height of the full sized image.
As others suggested, last two parameters should be original size of the image.
If $dir is your filename, you can use getimagesize to obtain picture's original dimensions.
You can use imagecopyresized or imagecopyresampled. Difference is that imagecopyresized will copy and resize while imagecopyresampled will also resample your image which will yield better quality.
<?php
$dir = $_GET['dir'];
header('Content-type: image/jpeg');
$create = imagecreatetruecolor(150, 150);
$img = imagecreatefromjpeg($dir);
list($width, $height) = getimagesize($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);
imagejpeg($create, null, 100);
?>

Categories