Hi Guys,
Hit an annoying problem with the imagecopyresampled(); function, it's not resizing the image whatsoever. Thanks in advance!
These are my variables:
output_name: water-lilies.jpg
output_path: images/thumbs/
path: images/water-lilies.jpg
target_width:100
target_height:100
compression:80
debug:1
and this is the function, ignore the error function.
function resizeImage($output_name,$output_path,$path,$target_width,$target_height,$compression=null,$debug=false)
{
global $error;
if($debug)
{
echo "\n<table><thead><tr><th>Variable</th><th>Value</th></tr></thead>";
echo "\n<tr><td>output_name</td><td>".$output_name."</td></tr>";
echo "\n<tr class='odd'><td>output_path</td><td>".$output_path."</td></tr>";
echo "\n<tr><td>path</td><td>".$path."</td></tr>";
echo "\n<tr class='odd'><td>target_width</td><td>".$target_width."</td></tr>";
echo "\n<tr><td>target_height</td><td>".$target_height."</td></tr>";
echo "\n<tr class='odd'><td>compression</td><td>".$compression."</td></tr>";
echo "\n<tr><td>debug</td><td>".$debug."</td></tr>";
echo "\n</table>";
}
// What image type is it?
if (preg_match("/.jpg/i", "$path")) $format = 'image/jpeg';
else if (preg_match("/.gif/i", "$path")) $format = 'image/gif';
else if (preg_match("/.png/i", "$path")) $format = 'image/png';
else $error->addError("Image type not supported.");
if(!$error->checkForErrors())
{
// Assign $format; with image type
switch($format)
{
case 'image/jpeg':
$path_image = imagecreatefromjpeg($path);
break;
case 'image/gif';
$path_image = imagecreatefromgif($path);
break;
case 'image/png':
$path_image = imagecreatefrompng($path);
break;
}
if(!file_exists($path))
{
$error->addError("Image supplied does not exist.");
}
else
{
// Get properties of existing image.
list($width_orig, $height_orig) = getimagesize($path);
// Find original image ratio for constrained scaling.
$ratio_orig = $width_orig/$height_orig;
if ($target_width > $width_orig
&& $target_height > $height_orig) $error->addError("Image is too small to resize");
else if ($target_width > $width_orig) $target_width = $width_orig;
else if ($target_height > $height_orig) $target_height = $target_height;
else if ($target_width/$target_height > $ratio_orig) $target_width = $target_height*$ratio_orig;
else $target_height = $target_width/$ratio_orig;
if(!$error->checkForErrors())
{
$image_output = imagecreatetruecolor($target_width, $target_height);
// Try and create the image with all the resized values.
if(!imagecopyresampled($image_output,$path_image,0,0,0,0,$target_width,$target_height,$width_orig,$height_orig)) $error->addError("Image failed to resize.");
// Save the image, check what format it is first!
switch($format)
{
case 'image/jpeg':
if($compression == null || !is_int($compression) || $compression > 100 || $compression < 1) $compression = 80;
imagejpeg($path_image,$output_name,$compression);
break;
case 'image/gif';
imagegif($path_image,$output_name);
break;
case 'image/png':
imagepng($path_image,$output_name);
break;
}
// Now destroy the memory hogging temp files
imagedestroy($path_image);
}
}
}
if($error->checkForErrors()) return true;
else return false;
}
Server info:
Apache/2.0.58 (Win32) PHP/5.1.4
GD Version bundled (2.0.28 compatible)
Thanks again!