PDA

View Full Version : resizing attachments with external gd code


Bernd
05-14-2006, 06:50 PM
I'm trying to resize thumbnails with external gd code, but it seems the images are not correctly read.

The image link would look like this.

tn_image.php?image=attachment.php?attachmentid=56

The code below is saved in tn_image.php

<?php
// make thmubnail of image
function sizeImage($image, $x, $y, $proportional) {
if (!$attr = getimagesize($image)) {
trigger_error("GD: Image does not exist. Must be gif, jpeg, or png!",E_USER_ERROR);
}

switch ($attr[2]) {
case 1:
$image = imagecreatefromgif($image);
break;
case 2:
$image = imagecreatefromjpeg($image);
break;
case 3:
$image = imagecreatefrompng($image);
break;
default:
trigger_error("GD: Image type wrong. Must be gif, jpeg, or png!", E_USER_ERROR);
}
if ($proportional) {
if ($attr[0]<$attr[1]){
$x = $y * ($attr[0]/$attr[1]);
}
else{
$y = $x / ($attr[0]/$attr[1]);
}
}
$newimage = imagecreatetruecolor($x,$y);
imagecopyresampled($newimage, $image, 0, 0, 0, 0, $x, $y, $attr[0], $attr[1]);
imagepng($newimage);
imagedestroy($image);
imagedestroy($newimage);
}
$image = $_GET['image']; //image location
$x = 150; //width
$y = 100; //height
$proportional = TRUE; //proportional or not
header("Content-type: image/png"); //so we can use the image right in a tag. <img src="image.php?image=me.gif">
sizeImage($image, $x, $y, $proportional);
?>

I already know that attachment.php?attachmentid=56 in the link is the issue, but I have no idea how to solve this. It it actually required to save the image to the hd first before any attachment can be resized? I'd rather do it on the fly.

Alan @ CIT
05-14-2006, 08:48 PM
I suspect your script is just treating "attachment.php" as an image file, and ignoring everything else.

Take a look at www.php.net/fopen for examples of how URLs should look when your trying to open a remote file.

I'm also not convined that PHP can open remote files when arguments are involved. Probably wrong on that one though.

Thanks,
Alan.