Reputation: 595
I'm currently writing a front end for a PHP script(not written by me) that resizes images(PNG,GIF,JPG) and saves them as JPEGs. Its very simple and works fine when inputting JPEGs, but seems not to work for PNG or GIF images.
Here's the code for the resizer:
<?php
header ("Content-type: image/jpeg");
$img = $_GET['img'];
header("Content-Disposition: attachment; filename=resized-$img");
$percent = $_GET['percent'];
$constrain = $_GET['constrain'];
$w = $_GET['w'];
$h = $_GET['h'];
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
// image height
$sh = $x[1];
if ($percent > 0) {
// calculate resized height and width if percent is defined
$percent = $percent * 0.01;
$w = $sw * $percent;
$h = $sh * $percent;
} else {
if (isset ($w) AND !isset ($h)) {
// autocompute height if only width is set
$h = (100 / ($sw / $w)) * .01;
$h = @round ($sh * $h);
} elseif (isset ($h) AND !isset ($w)) {
// autocompute width if only height is set
$w = (100 / ($sh / $h)) * .01;
$w = @round ($sw * $w);
} elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
// get the smaller resulting image dimension if both height
// and width are set and $constrain is also set
$hx = (100 / ($sw / $w)) * .01;
$hx = @round ($sh * $hx);
$wx = (100 / ($sh / $h)) * .01;
$wx = @round ($sw * $wx);
if ($hx < $h) {
$h = (100 / ($sw / $w)) * .01;
$h = @round ($sh * $h);
} else {
$w = (100 / ($sh / $h)) * .01;
$w = @round ($sw * $w);
}
}
}
$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = @ImageCreateFromPNG ($img) or // or PNG Image
$im = @ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF
if (!$im) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile ($img);
} else {
// Create the resized image destination
$thumb = @ImageCreateTrueColor ($w, $h);
// Copy from image source, resize it, and paste to image destination
@ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
// Output resized image
@ImageJPEG ($thumb);
}
?>
Can you see any reason why the png/gif options are not working? I have GD up-to-date and enabled for all formats and am running php 5.3.3
Thanks in advance.
Upvotes: 1
Views: 1036
Reputation: 980
In addition to what Francois Deschenes allready said... Please remove all those @
and turn error reporting on (error_reporting(E_ALL);
). You supressed so much sources of possible errors, that it is nearly impossible to debug this script yourself. Try to write scripts that don't even use a single @
anywhere...
Upvotes: 0
Reputation: 360762
Try imagecreatefromstring(file_get_contents($img))
instead of that hideous construct of yours. Or do a proper switch statement and explicitly determine which one to use instead of trying each in turn.
Upvotes: 0
Reputation: 24989
The third item of your $x
array from the getimagesize()
contains the mime-type of the image. You should use it instead of using the or
statement and trying all types.
switch ( $x[2] )
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
Upvotes: 3