-
imagecopyresampled issue
I've made function that resizes a given image, but when I change imagecopyresized with imagecopyresampled I'm encountering some problems.
In Firefox I get a download prompt for the php file I'm running. In IE it says I can't display the page.
I use the folowing code:
PHP Code:
// create a new image-resource
$thumb = imagecreatetruecolor($new_width, $new_height);
$new_source = imagecreatefromjpeg($source);
// Creates the thumbnail
imagecopyresampled($thumb, $new_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
imagejpeg($thumb, $target, 100);
If I change it back to imagecopyresized it works just fine. Anyone have any idea why this happens?
-
1 Attachment(s)
Are you sending any content headers?
Try
Code:
header("Content-type: image/jpeg");
I have attached a script that I have written in the past which takes in a path, maximum height and maximum width (through GET inputs) and outputs the image which the correct headers (GIF and JPEG currently). Feel free to use it as reference or the whole thing if you want.
(Sorry its a .txt file cos the forum wouldn't let me upload a php file - you might want to change the extension back)
-
Hi lix0r,
I have tried this on my localhost:
PHP Code:
<?php
header("content-type: image/jpeg");
$source = "Landschaft.jpg";
// create a new image-resource
$new_width = 64;
$new_height = 64;
$thumb = imagecreatetruecolor($new_width, $new_height);
$new_source = imagecreatefromjpeg($source);
$srcsize = getimagesize("Landschaft.jpg");
// Creates the thumbnail
imagecopyresized($thumb, $new_source, 0, 0, 0, 0, $new_width, $new_height, $srcsize[0], $srcsize[1]);
$target = "test.jpg";
imagejpeg($thumb, "", 100);
?>
This works fine for me whether I use imagecopyresized or imagecopyresampled.
Hope this helps somehow.
Sven Schoene
-
Thanks for the response. I have tried with an image-header, but it didn't work. It shouldn't be a header related problem, since I'm saving the image to a file and not outputting it directly to the browser.
I've also tried with a text/html header, wiht no luck.
-
What is the line:
Code:
$new_width, $new_height, $img_width, $img_height);
That looks like it might screw things up quite a bit!
-
just a mistake I did when I coppied it over here from the script :S
-
Can the filesize be causing this?
-
Is this all the code for the function? If not can you post the whole thing?
Also, have you tried the code Sven S. posted?
-
Is it something simple like a different version of GD?
It works OK for me and I have 2.0.28 compatible.
Also it would be interesting to see where you are getting your variables.
The thing I would do is get the code to work independent of everything else to ensure it works on its own using Sven S.'s code. Then worry about getting it to work in the rest of your code.
-
I did think it might be GD too. imagejpeg() requires at least version 1.8. It might be worth checking out just to be sure.
-
The code is the same as Sven's, I just have som extra calculations of the new width and height.
Anyways, here is the whole function:
PHP Code:
function resize_image($resize_limit, $source, $target) {
list($img_width, $img_height, $img_type, $attr) = getimagesize($source);
if ($img_width <= $img_height) {
$new_height = $resize_limit;
// Calculate the thunmbnail sizes
$ratio = (int) $img_width / (int) $img_height;
$new_width = ceil( (int) $resize_limit * (float) $ratio );
}
if ($img_height <= $img_width) {
$new_width = $resize_limit;
// Calculate the thunmbnail sizes
$ratio = (int) $img_height / (int) $img_width;
$new_height = ceil( (int) $resize_limit * (float) $ratio );
}
if ($img_width == $img_height) {
// Sets the correct height and width
$new_width = $resize_limit;
$new_height = $resize_limit;
}
// create a new image-resource
$thumb = imagecreatetruecolor($new_width, $new_height);
$new_source = imagecreatefromjpeg($source);
// Creates the thumbnail
imagecopyresampled($thumb, $new_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
imagejpeg($thumb, $target, 100);
}
-
Well that exact code works for me. There are several more things to check though:
- The file you are reading from is readable
- The file/directory you are trying to write to is writable
- You have a new enough version of GD
- The file you are reading from is a JPEG file and not something else
-
The files and directories are readable, and I have the GD version that is bundled with php5. All files that are uploaded must be jpg's, so that shouldn't be the problem.
If it works fine on your server, I guess it somthing wrong with mine. Thanks for the help everybody :)
-
Turn on the error reporting in this particular script and see if there is an output. Try FireBug (or some similar) firefox exception that will tell you what are the headers like. I'm guessing the PHP is throwing a fatal error of some sort or the process (php) could be terminated by some Segmentation Fault. The latter would explain why a file download is being forced upon you -- apache doesn't handle the request like it should. Happened to me a few times.
Now for a shameless plug: you could try WideImage, an easy to use image library (php5). With that, the resize code could look like this:
PHP Code:
$img = wiImage::load('Landschaft.jpg');
echo $img->resize(64, 64)->asString('jpeg', 80);
regards
-
I am not sure if you already solved your image resizing issue, but for you and all other devs like you looking for a way to transform images on the fly, you might want to check the open-source project Asido: there are a lot of useful features (http://www.asido.info/about/features/), including various types of image resize, watermarking, image copy, cropping, etc. It can use various platforms: not only GD but ImageMagick too (either via shell or via any of the php extensions like php_magickwand.dll or php_imagick.dll). Here's the list of drivers: http://www.asido.info/about/drivers/