Here is a function that i use to upload images submitted by users of a web page:
function uploadimg($image_path, $image_id, $new_w, $new_h, $compression)
{
$src_img = imagecreatefromJPEG("$image_path/$image_id.jpg");
list($width, $height, $type, $attr) = getimagesize("$image_path/$image_id.jpg");
$w_ratio=$width/$new_w;
$new_h=$height/$w_ratio;
/*if picture is wider than 540 than resize it*/
if($width > 540){
$w_ratio=$width/540;
$height=$height/$w_ratio;
$width=540;
}
/*if picture is wider than 540 than resize it*/
$dst_img = imagecreatetruecolor($new_w,$new_h);
$dst_img2 = imagecreatetruecolor($width,$height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagecopyresampled($dst_img2, $src_img, 0, 0, 0, 0, $width,$height, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, "$image_path/s_$image_id.jpg", $compression);
imagejpeg($dst_img2, "$image_path/$image_id.jpg", 80);
chmod ("$image_path/$image_id.jpg", 0644);
chmod ("$image_path/s_$image_id.jpg", 0644);
}
This function not only uploads images to given path but also resizes it to fit in a 540 px width table if it's more than 540 px in width. Any way it works fine with photos that are less than 300 kB and works strange with others, for example it stops changing the chmod of a picture and does not create a thumb (s_ file) it supposed to.
Maybe anyone can look through it?
function uploadimg($image_path, $image_id, $new_w, $new_h, $compression)
{
$src_img = imagecreatefromJPEG("$image_path/$image_id.jpg");
list($width, $height, $type, $attr) = getimagesize("$image_path/$image_id.jpg");
$w_ratio=$width/$new_w;
$new_h=$height/$w_ratio;
/*if picture is wider than 540 than resize it*/
if($width > 540){
$w_ratio=$width/540;
$height=$height/$w_ratio;
$width=540;
}
/*if picture is wider than 540 than resize it*/
$dst_img = imagecreatetruecolor($new_w,$new_h);
$dst_img2 = imagecreatetruecolor($width,$height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagecopyresampled($dst_img2, $src_img, 0, 0, 0, 0, $width,$height, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, "$image_path/s_$image_id.jpg", $compression);
imagejpeg($dst_img2, "$image_path/$image_id.jpg", 80);
chmod ("$image_path/$image_id.jpg", 0644);
chmod ("$image_path/s_$image_id.jpg", 0644);
}
This function not only uploads images to given path but also resizes it to fit in a 540 px width table if it's more than 540 px in width. Any way it works fine with photos that are less than 300 kB and works strange with others, for example it stops changing the chmod of a picture and does not create a thumb (s_ file) it supposed to.
Maybe anyone can look through it?