Jump to content

Image resize issue


son

Recommended Posts

I have a file resize function running for three separate sizes and run into trouble whenever I try to upload an image which is smaller than the specified THUMB_SIZE (width and/or height, for example 49px by 399px). Images move ok for BASKET_SIZE and IMG_SIZE, but do not appear for THUMB_SIZE.The function is:

function resize_image($src, $dst, $width, $height){  if (file_exists($src))  {    $chunks = explode('.', $src);    $ext = $chunks[count($chunks) - 1];    if ($ext == 'jpg' OR $ext == 'JPG')      $orig = imagecreatefromjpeg($src);    elseif ($ext == 'gif' OR $ext == 'GIF')      $orig = imagecreatefromgif($src);    elseif ($ext == 'png' OR $ext == 'PNG')      $orig = imagecreatefrompng($src);    else    {      echo "Unspported extension ({$ext})";      return;    }    $w = imagesx($orig);    $h = imagesy($orig);    // if it needs to be resized    if ($w > $width || $h > $height)    {      // if the width is larger than allowed      if ($w > $width)      {        $ratio = $width / $w; // ratio is max width divided by actual (<1)        $img = imagecreatetruecolor($width, round($h * $ratio));  // height is actual height times ratio (less)        imagecopyresampled($img, $orig, 0, 0, 0, 0, $width, round($h * $ratio), $w, $h);        $orig = $img;        $w = imagesx($orig);        $h = imagesy($orig);      }      // if the height is larger than allowed      if ($h > $height)      {        $ratio = $height / $h;  // ratio is max height divided by actual (<1)        $img2 = imagecreatetruecolor(round($w * $ratio), $height); // width is actual width times ratio (less)        imagecopyresampled($img2, $orig, 0, 0, 0, 0, round($w * $ratio), $height, $w, $h);        $orig = $img2;        $w = imagesx($orig);        $h = imagesy($orig);      }        switch ($ext)      {        case 'jpg':          imagejpeg($orig, $dst);          break;        case 'gif':          imagegif($orig, $dst);          break;        case 'png':          imagepng($orig, $dst);          break;        case 'JPG':          imagejpeg($orig, $dst);          break;        case 'GIF':          imagegif($orig, $dst);          break;        case 'PNG':          imagepng($orig, $dst);          break;      }    }    imagedestroy($orig);  }  else    echo "Error: the source file does not exist ({$src}, {$dst})";}

I specify in config file the sizes as:define('BASKET_SIZE_W',88);define('BASKET_SIZE_H',88);define('THUMB_SIZE_W',50);define('THUMB_SIZE_H',400);define('IMG_SIZE_W',600);define('IMG_SIZE_H',600);and move the whole lot on upload web page via:

							foreach ($images as $img_id => $img_name)							{								if (!move_uploaded_file($_FILES[$img_id]['tmp_name'], "../products/main/{$id}-{$$img_id}"))				  				$errors[$img_id] = 'The file could not be moved';								else								{								resize_image("{$main_img}/{$id}-{$$img_id}", "{$main_img}/{$id}-{$$img_id}", IMG_SIZE_W, IMG_SIZE_H);								resize_image("{$main_img}/{$id}-{$$img_id}", "{$preview_img}/{$id}-{$$img_id}", THUMB_SIZE_W, THUMB_SIZE_H);								resize_image("{$main_img}/{$id}-{$$img_id}", "{$basket_img}/{$id}-{$$img_id}", BASKET_SIZE_W, BASKET_SIZE_H);								}							}

Where am I going wrong?Son

Link to comment
Share on other sites

First, is this path:../products/main/{$id}-{$$img_id}the same as this:{$main_img}/{$id}-{$$img_id}Second, are you intending to use the variable variable there? I can't tell from the code what $img_id would be set to.Third, since you resize the IMG_SIZE sizes first, and overwrite the original file with the new one, would that cause it to not resize the other one? I'm confused about what specifically the problem is, when you say that some images move OK and some don't appear, are you saying that the resize_image function doesn't save one of the images? If so, are you giving it the correct save path?

Link to comment
Share on other sites

You are right, to use../products/main/{$id}-{$$img_id}and{$main_img}/{$id}-{$$img_id}is not very consistent (I knew it was the same) and changed the first instance also to:{$main_img}/{$id}-{$$img_id}I did not give any further code snippet as the code works perfectly well as long as image that is uploaded for image 7 is:- width bigger than THUMB_SIZE_W and height bigger than THUMB_SIZE_H- width bigger than THUMB_SIZE_W and height smaller than THUMB_SIZE_H- width smaller than THUMB_SIZE_W and height bigger than THUMB_SIZE_HIf you upload an image for image 7 which is:- width smaller than THUMB_SIZE_W and height smaller than THUMB_SIZE_Hit won't resize and move the resized the file into the specified folder for thumb size (but will work fine for all other files). In general, the other images and sizes for basket and main images do not share this problem...However, the code that specifies the name is:

 $images = array(  'img1' => 'Image 1',  'img2' => 'Image 2',  'img3' => 'Image 3',  'img4' => 'Image 4',  'img5' => 'Image 5',  'img6' => 'Image 6',  'img7' => 'Image 7');		foreach ($images as $img_id => $img_name)		{  			if (!isset($_FILES[$img_id]['name']) OR empty($_FILES[$img_id]['name']) OR $_FILES[$img_id]['error'] == 4)   			{  				if ($img_id == 'img1' || $img_id == 'img7')				{    			$errors[$img_id] = $img_name;				}  				if ($img_id == 'img7')				{    			$errors[$img_id] = "Image for overview page";				}    			$$img_id = FALSE;  				}   				else   				{    			if ($_FILES[$img_id]['error'] != 0)    			{      			$errors[$img_id] = upload_error($_FILES[$img_id]['error']);      			$$img_id = false;    			}    			else    			{      			$ext = explode('.',$_FILES[$img_id]['name']);      			$ext = $ext[count($ext)-1];      				if (!in_array(strtolower($ext), $allowed))      				{        			$errors[$img_id] = 'Allowed formats: jpg, gif, png';        			$$img_id = FALSE;      				}      				else      				{      				$img_num = trim(str_replace('Image', '', $img_name));         			$$img_id = "{$img_num}.{$ext}";        			$$img_id = strtolower($$img_id);      				}    			}  			}		}

I hope I make more sense now...Son

Link to comment
Share on other sites

The resize_image function has an if statement to only resize the image if the width and height meet certain conditions, so I would assume that if statement is not evaluating to true for the images that aren't getting saved.

Link to comment
Share on other sites

The resize_image function has an if statement to only resize the image if the width and height meet certain conditions, so I would assume that if statement is not evaluating to true for the images that aren't getting saved.
But this is not what this function is doing. It checks if width and/or height are larger for main, preview and basket and if so, resizes to the maximum before it moves the files in relevant folders. If width and height are within given maximum values it does not resize before it moves the files in relevant folders. It works well for the main image size for example. To illustrate: if I upload an image which is 330px by 34px, it is smaller than max for main and preview size and no resize should happen for those. As it is larger than max size values for basket it resizes before moving one copy to basket folder. basket and main image get moved ok, only that there is no trace of preview in this case (height and width lower than given max values for preview)... This really does not make any sense to me.I hope you can follow me,Son
Link to comment
Share on other sites

I understand what you're saying, but just look at the code. There are only a few situations where the function will not save a file. One of those situations is happening.1: if the source file does not exist, will output a message saying so2: if the extension is not valid, will output a message saying so3: if the dimensions are smaller than the maximum, does not print a messageIf the file isn't getting saved, then one of those three conditions is true. If none of those conditions were true, then the file would get saved. If you're not seeing a message get printed, there's only one situation where the function will quit without saving a file and also without printing a message.

Link to comment
Share on other sites

I understand what you're saying, but just look at the code. There are only a few situations where the function will not save a file. One of those situations is happening.1: if the source file does not exist, will output a message saying so2: if the extension is not valid, will output a message saying so3: if the dimensions are smaller than the maximum, does not print a messageIf the file isn't getting saved, then one of those three conditions is true. If none of those conditions were true, then the file would get saved. If you're not seeing a message get printed, there's only one situation where the function will quit without saving a file and also without printing a message.
But for 3: if the dimensions are smaller than the maximum, does not print a message(which would be the relevant case)it should just move the file as it is into relevant folder. Also, to re-iterate: The overview image is the only size causing an issue when width and height are smaller than given max width and max height for overview. It works perfectly well for main image size for example (which is even larger than the troublesome size). This does not make any sense, does it? I think I am completely lost now...Son
Link to comment
Share on other sites

it should just move the file as it is into relevant folder
That's not what the code does. Look at the if statement which checks both the width and height. Look at what gets executed if the if statement evaluates to false.
I think I am completely lost now...
All you need to do is follow the code, it's telling you exactly what is happening.
Link to comment
Share on other sites

  • 3 months later...
That's not what the code does. Look at the if statement which checks both the width and height. Look at what gets executed if the if statement evaluates to false.All you need to do is follow the code, it's telling you exactly what is happening.
Thanks, got it working now...SonBy the way, just to say: sorry for getting back so late. Cought (again) a nasty something that took ages to fade away (and so does my money;-)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...