Hi
I know I have been going round the houses with this image resizing quite a bit…but still trying to do it. Some progress has been made. This is not using Imagick, as I have no idea about this library.
So what the below code is now doing is picking up the attributes of the images being selected by the user and then reducing them to a set size (via sqrt etc). What I’m trying to achieve is for any image loaded to be reduced to around 32kb in size.
What is happening is the following:
.gif images - these are working great, and any .gif image loaded is being reduced to sizes between 30-35kb.
.png images - also reasonably consistent, although slightly too small sometimes.
Main Issue - .jpg/.jpeg images…
.jpg images which are already quite small in filesize are being reduced and the image thumbnail displays to the user fine.
.jpg images which are large (i.e. the ones that actually really need to be reduced) are usually coming out at 3kb in filesize, and the thumbnail is just a black image, no picture showing.
Or alternatively the script doesn’t work at all and nothing comes back to the page as it just freezes…
What I’m now thinking is that the only attribute of the original image that I don’t have is the actual quality of the picture. This might be messing up the jpg’s? If I could know the quality of incoming images, then I could set the quality in my resizing function. - although I really am just clutching at straws now…
If anyone could shed some light on this or help me to understand what is missing or why the .jpg don’t resize property that would be great.
(apologies for really long script - but only because 7 images are being loaded and part of the issue so far seems to be that I getimagesize() or something in the code wasn’t liking the repitition. This is now working fine, and I will put it all in function when I understand what else needs to be done…
[php]
function createfilename($ID,$image_name)
{
$output = "Start";
if (!empty($image_name))
{
$output = number_format($ID,0,".",",") . '_' . time() . '_' . mt_rand() .$image_name;
}
else
{
$output = "No Image";
}
return $output;
}
function checkfile($image_name,$type,$size,$tmp_name,$error,$old_width,$old_height)
{
$output ='start';
if (!empty($image_name))
{
if (
(($type == 'image/gif') || ($type == 'image/jpg') || ($type == 'image/pjpeg') || ($type == 'image/png') || ($type == 'image/jpeg'))
&& (($size > 0) && ($size <= GW_MAXFILESIZE))
&& ($error == '0'))
{
// Move the files to the target upload folder
$target = GW_UPLOADPATH . $image_name;
if (move_uploaded_file($tmp_name , $target) ) {$output = 'success';} else {$output = 'load error'; }
}
elseif ($size > GW_MAXFILESIZE){
$target1 = GW_UPLOADPATH . 'original' . $image_name ;
$target2 = GW_UPLOADPATH . $image_name ;
// leave this line in if you want to keep original
// if (move_uploaded_file($tmp_name , $target1) ) {$output = ‘success’;} else {$output = ‘load error’; }
$reductionfactor = sqrt ( $size / GW_MAXFILESIZE ) ;
$new_height = number_format( $old_height / $reductionfactor ,0 , '.', '');
$new_width = number_format( $old_width / $reductionfactor ,0 , '.', '');
$new_image = imagecreatetruecolor($new_width, $new_height);
$image_info = getimagesize($filename);
if(( $type == 'image/jpg' )|| ($type == 'image/pjpeg') || ($type == 'image/jpeg')){
imagejpeg($new_image,$target2,85);
$old_image = imagecreatefromjpeg($tmp_name);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); }
elseif( $type == 'image/gif' ) {
$old_image = imagecreatefromgif($tmp_name);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
imagegif($new_image,$target2); }
elseif( $type == 'image/png' ) {
$old_image = imagecreatefrompng($tmp_name);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
imagepng($new_image,$target2); }
$output='success';
}
else { $output='error'; }
}
else { $output='success'; }
return $output;
}
?>[/php]