I'm probably doing something stupid, but this code works when it isn't a function. I'll show you what I mean. This works fine:
$filename = 'mod_perl.jpg';
$type = preg_replace('#.*?.(.+)$#i', '\1', $filename);
if ($type == 'jpg') {
$type = 'jpeg';
}
$func = 'imagecreatefrom' . $type;
// Content type
//header('Content-type: image/' . $type);
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width $percent;
$new_height = $height $percent;
$max_width = 200;
Calculate percent
$per = $width/$max_width;
$new_width = $width / $per;
$new_height = $height / $per;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height) or die ('imagecreatetruecolor failed');
$image = $func($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height) or die ('imagecopy failed');
// Output
imagejpeg($image_p, 'template.jpeg', 100) or die ('imagejpg failed');
It resizes the mod_perl.jpg image to a width of 200 and proportional height, and saves the image to template.jpeg. But when I make it a function, this doesn't work.
function resize($filename)
$type = preg_replace('#.*?.(.+)$#i', '\1', $filename);
if ($type == 'jpg') {
$type = 'jpeg';
}
$func = 'imagecreatefrom' . $type;
// Content type
//header('Content-type: image/' . $type);
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width $percent;
$new_height = $height $percent;
$max_width = 200;
Calculate percent
$per = $width/$max_width;
$new_width = $width / $per;
$new_height = $height / $per;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height) or die ('imagecreatetruecolor failed');
$image = $func($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height) or die ('imagecopy failed');
// Output
imagejpeg($image_p, 'template.jpeg', 100) or die ('imagejpg failed');
}
resize('mod_perl.jpg') or die ('Could not execute');
Now the strange thing is none of the "or die()" statements are executed. Please help.