I found Ian Atkin's solution here - Resize image in PHP - and am trying to modify it to accommodate png and gif, and to save the resized image. Jpg works, but I'm getting black images (of correct size) for png and gif. I can't figure out what am I doing wrong. Here's the code:
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
if($extension=="gif") {
$src = imagecreatefromgif($file);
} elseif($extension=="png") {
$src = imagecreatefrompng($file);
} else {
$src = imagecreatefromjpeg($file);
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if($extension=="gif") {
imagegif($dst, $file);
} elseif($extension=="png") {
imagepng($dst, $file);
} else {
imagejpeg($dst, $file);
}
return $dst;
}
Thank you very much!
I use this code in images.php to show the images :
imagepng($out);
and if i want to reduce the images quality for thumbnails i tried this code :
imagepng($out,$filename,$quality);
i want to know how to reduce image quality without saving.
function resize_image($file, $w, $h, $crop=false) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
//Get file extension
$exploding = explode(".",$file);
$ext = end($exploding);
switch($ext){
case "png":
$src = imagecreatefrompng($file);
break;
case "jpeg":
case "jpg":
$src = imagecreatefromjpeg($file);
break;
case "gif":
$src = imagecreatefromgif($file);
break;
default:
$src = imagecreatefromjpeg($file);
break;
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
$filename = "/var/www/images/mynormalimage.png";
$resizedFilename = "/var/www/images/myresizedimage.png";
// resize the image with 300x300
$imgData = resize_image($filename, 300, 300);
// save the image on the given filename
imagepng($imgData, $resizedFilename);
// or according to the original format, use another method
// imagejpeg($imgData, $resizedFilename);
// imagegif($imgData, $resizedFilename);
https://ourcodeworld.com/articles/read/197/how-to-resize-an-image-and-reduce-quality-in-php-without-imagickenter code here
I am trying to resize and image with the following function:
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
After creating the function, I am trying to display the image after resizing by using this code but it is not working:
<?php
$img = resize_image('../images/avatar/demo.jpg', 120, 120);
var_dump($img); //The result is: resource(6, gd)
?>
<img src="<?php echo $img;?>"/>
PS: There is no problem with the inclusion of the function
You can't directly output an image that way. You can either:
Save the image to disk and enter the URL in the image tag.
Buffer the raw data, base64 encode it, and output it as a data URI (I wouldn't recommend this if you're working with large images.).
Approach 1:
<?php
$img = resize_image('../images/avatar/demo.jpg', 120, 120);
imagejpeg($img, '../images/avatar/demo-resized.jpg');
?>
<img src="<?= 'www.example.com/images/avatar/demo-resized.jpg' ?>"/>
Approach 2:
<?php
$img = resize_image('../images/avatar/demo.jpg', 120, 120);
ob_start();
imagejpeg($img);
$output = base64_encode(ob_get_contents());
ob_end_clean();
?>
<img src="data:image/jpeg;base64,<?= $output; ?>"/>
I'm trying to write a simple file uploader which can resize an image if needed and copy it into the server's file system. That works if I pass the file directly but I also want to be able to pass a URL via a GET parameter. And somehow imagecopyresampled seems to fail with the URL.
Here is my code:
if(isset($_FILES["file"]))
$fn = $_FILES['file']['tmp_name'];
else
$fn = urldecode($_GET['url']);
$fileMD5 = md5_file($fn);
$target = $basedir . $fileMD5 . ".png";
list($width, $height, $imgtype) = getimagesize($fn);
if ($imgtype == IMAGETYPE_PNG)
$img = imagecreatefrompng($fn);
else if ($imgtype == IMAGETYPE_JPEG)
$img = imagecreatefromjpeg($fn);
else if ($imgtype == IMAGETYPE_GIF)
$img = imagecreatefromgif($fn);
else if ($imgtype == IMAGETYPE_BMP)
$img = imagecreatefromwbmp($fn);
else {
echo "unsupported file format";
return;
}
// image resize
if($width > $maxwidth && $width >= $height) {
$newwidth = $maxwidth;
$newheight = ($height / $width) * $newwidth;
} else if($height > $maxheight) {
$newheight = $maxheight;
$newwidth = ($width / $height) * $newheight;
}
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($tmp, $target);
Everything seems to work fine until imagecopyresampled. I get the correct IMAGETYPE and also, something is created with imagecreatetruecolor. But imagecopyresampled returns false. I'm pretty confused since the script seems to be able to actually read the image and get it's first bits to determine the type of it.
Any suspections what's going wrong?
Thanks in advance.
Your resize section seems to leave out some situations that could lead to an undefined $newheight and $newwidth.
Are you sure about imagecreatetruecolor returning an image?
I'm wanting to create a very very basic upload, resize, and crop PHP script.
The functionality to this will be identical (last i checked anyway) to the method Twitter uses to upload avatar pictures.
I want the script to take any size image, resize the shortest side to 116px, then crop off the top and bottom (or left and right side if it's landscape) as to get a square 116px by 116px.
I don't want a bloated PHP script with client side resizing or anything, just a simple PHP resize and crop. How is this done?
The GD Library is a good place to start.
http://www.php.net/manual/en/book.image.php
There a simple to use, open source library called PHP Image Magician. It uses GD but simplifies it's usage to 3 lines.
Example of basis usage:
$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');
If you want an example to work from my upload, resize and crop class does all of this plus some other cool stuff - you can use it all if needed or just take the bits out that you like:
http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/
I don't think it is too bloated! - you can just do something this (not tested):
if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it
include('INCLUDE_CLASS_FILE_HERE.php');
$myImage = new _image;
// upload image
$myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE
$myImage->returnType = 'array'; // RETURN ARRAY OF IMAGE DETAILS
$img = $myImage->upload($_FILES['file']);
if($img) {
$myImage->newWidth = 116;
$myImage->newHeight = 116;
$i = $myImage->resize(); // resizes to 116px keeping aspect ratio
// get new image height
$imgWidth = $i['width'];
// get new image width
$imgHeight = $i['height'];
if($i) {
// work out where to crop it
$cropX = ($imgWidth>116) ? (($imgWidth-116)/2) : 0;
$cropY = ($imgHeight>116) ? (($imgHeight-116)/2) : 0;
$cropped = $myImage->crop(116,116,$cropX,$cropY);
if($cropped) { echo 'It Worked (I think!)'; print_r($cropped);
} else { echo 'Crop failed'; }
} else { echo 'Resize failed'; }
} else { echo 'Upload failed'; }
I made this simple function which is very easy to use, it allows you to resize, crop and center an image to a specific width and height, it can suppert JPGs, PNGs and GIFs. Feel free to copy and paste it to your code:
function resize_imagejpg($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromjpeg($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,
$width, $height);
imagejpeg($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function resize_imagegif($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromgif($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
$background = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,
$width, $height);
imagegif($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function resize_imagepng($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefrompng($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
$background = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth,
$newheight,$width, $height);
imagepng($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function ImageResize($file, $w, $h, $finaldst) {
$getsize = getimagesize($file);
$image_type = $getsize[2];
if( $image_type == IMAGETYPE_JPEG) {
resize_imagejpg($file, $w, $h, $finaldst);
} elseif( $image_type == IMAGETYPE_GIF ) {
resize_imagegif($file, $w, $h, $finaldst);
} elseif( $image_type == IMAGETYPE_PNG ) {
resize_imagepng($file, $w, $h, $finaldst);
}
}
All you have to do to use it is call it like so:
ImageResize(image, width, height, destination);
E.g.
ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");