This is in reference to my previos post but a completely different problem.
Previous problem and code can be found here:
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($up loadfile,$thumb file,120,100);
createthumb($up loadfile,$mainf ile,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($na me,$filename,$n ew_w,$new_h)
{
$system=explode (".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=ima gecreatefromjpe g($name);}
if (preg_match("/png/",$system[1])){$src_img=ima gecreatefrompng ($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=ImageC reateTrueColor( $thumb_w,$thumb _h);
imagecopyresamp led($dst_img,$s rc_img,0,0,0,0, $thumb_w,$thumb _h,$old_x,$old_ y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_i mg,$filename);
} else {
imagejpeg($dst_ img,$filename);
}
imagedestroy($d st_img);
imagedestroy($s rc_img);
}
[/php]
Previous problem and code can be found here:
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($up loadfile,$thumb file,120,100);
createthumb($up loadfile,$mainf ile,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($na me,$filename,$n ew_w,$new_h)
{
$system=explode (".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=ima gecreatefromjpe g($name);}
if (preg_match("/png/",$system[1])){$src_img=ima gecreatefrompng ($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=ImageC reateTrueColor( $thumb_w,$thumb _h);
imagecopyresamp led($dst_img,$s rc_img,0,0,0,0, $thumb_w,$thumb _h,$old_x,$old_ y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_i mg,$filename);
} else {
imagejpeg($dst_ img,$filename);
}
imagedestroy($d st_img);
imagedestroy($s rc_img);
}
[/php]
Comment