I have a script that I am using to scale original images on my server. Sometimes the script works perfect and sometimes it doesn't scale it properly. However, it looks like the black area of the target is correct it just didn't actually scale the image. Why is it so unpredictable?
// check
if($original_width > $original_height){ // landscape
// vars
$scale_ratio = $requested_width/$original_width;
$width = $requested_width;
$height = $original_height * $scale_ratio;
}
else if($original_height > $original_width){ // portrait
// vars
$scale_ratio = $requested_height/$original_height;
$width = $original_width * $scale_ratio;
$height = $requested_height;
}
else{ // square
// vars
$scale_ratio = $requested_height/$original_height;
$width = $original_width * $scale_ratio;
$height = $requested_height;
}
// create
$target = imagecreatetruecolor($width, $height);
$source = imagecreatefromjpeg($filename);
// copy
imagecopyresampled($target, $source, 0, 0, 0, 0, $width, $height, $original_width, $original_height);
// set
imagejpeg($target, null, $requested_quality);