This is in reference to my previos post but a completely different problem.
Previous problem and code can be found here:
http://www.thescripts.com/forum/thread563724.html
When I run the function like this, it gives me weird images (ratio is way off)
[php]
$uploaddir = '/home/avonprom/www/product_images/';
$thumbdir = '/home/avonprom/www/product_images/';
$uploadfile = $uploaddir . $_POST['sku'] . ".jpg";
$thumbfile = $thumbdir . "sm_" . $_POST['sku'] . ".jpg";
$mainfile = $thumbdir . "mid_" . $_POST['sku'] . ".jpg";
if (move_uploaded_file($_FILES['thumbnail']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
createthumb($uploadfile,$thumbfile,120,100);
createthumb($uploadfile,$mainfile,285,407);
} else {
echo "Possible file upload attack!\n";
}
[/php]
For example, one image was 285 x 450 which should have created a slightly smaller image due to 450 being too wide. Instead it came out 285 x 600 or something like that.
Does anyone see the error in calculation?
Thanks in advance
Aaron
EDIT:
In case you want to see the function here, here it is:
[php]
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
if ($new_w < $new_h)
{
$thumb_w=$new_w;
$thumb_h=$new_w;
}
elseif ($new_h < $new_w)
{
$thumb_w=$new_h;
$thumb_h=$new_h;
}
else
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thum b_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
[/php]