Harley1979 Posted August 15, 2008 Share Posted August 15, 2008 Got a problem with resizing an image. Here is my code, the image uploads fine but just seems to bypass the part where it resizes. Not a problem when creating thumbnails using the almost exact same method further on. Can anyone help? Thanks <?php $caption = $_REQUEST['caption']; $max_size = 8388608; $size = $_FILES['imagefile']['size']; $type = $_FILES['imagefile']['type']; $imageFile = $_FILES['imagefile']['tmp_name']; if(isset($imageFile)){ if ($type == "image/jpeg" || $type == "image/gif"){ $src = imagecreatefromjpeg($imageFile); list($origWidth, $origHeight) = getimagesize($imageFile); $path = "../gallery/"; $thumbPath = "../gallery/thumbs/"; // get last image from db $result = mysql_query("SELECT photo FROM gallery ORDER BY id desc LIMIT 1"); $row = mysql_fetch_row($result); // modify file name if(empty($row[0])){$photoName = 0;} $photoName = $row[0] + 1; $newName = $photoName.".jpg"; ///////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////// resize original image //////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// // if our image is not the desired dimension, resample to default dimensions if($origWidth < $origHeight && $origWidth > 560){ $newHeight = 540; $newWidth = $origWidth * ($newHeight/$origHeight); } elseif($origWidth > $origHeight && $origHeight > 540){ $newWidth = 560; $newHeight = $origHeight * ($newWidth/$origWidth); } else { $newWidth = $origWidth; $newHeight = $origHeight; } $mainImage = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($mainImage, $src, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); // save to path imagejpeg($mainImage, $path.$newName, 100); // destroy image imagedestroy($mainImage); ///////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////// create thumbnails //////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// // set crop size if($origWidth > $origHeight){ $biggestside = $origWidth; } else { $biggestside = $origHeight; } // crop size will be half that of the largest side $croppercent = .80; $cropwidth = $biggestside*$croppercent; $cropheight = $biggestside*$croppercent; // get top left coordinate $cl = array("x"=>($origWidth-$cropwidth)/2, "y"=>($origHeight-$cropheight)/2); // create thumbnail $thumbsize = 75; $thumb = imagecreatetruecolor($thumbsize, $thumbsize); imagecopyresampled($thumb, $src, 0, 0, $cl['x'], $cl['y'], $thumbsize, $thumbsize, $cropwidth, $cropheight); // save to path imagejpeg($thumb, $thumbPath.$newName, 100); imagedestroy($src); imagedestroy($thumb); if(move_uploaded_file($imageFile, $path.$newName)){ mysql_query("INSERT INTO gallery (photo, caption) VALUES ('$photoName', '$caption')") or die (mysql_error()); $imageMsg = "Image successfuly uploaded!"; } } else { $imageMsg = "Incorrect file type";} } ?> Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/ Share on other sites More sharing options...
Dragoa Posted August 15, 2008 Share Posted August 15, 2008 What are your parameters for wanting a resize? And what's the desired dimension? Also, you might want to use >= or <= in the checks between origHeight and origWidth, otherwise a square image will always remain the same. Even if it is 1000x1000. Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/#findComment-617536 Share on other sites More sharing options...
Harley1979 Posted August 15, 2008 Author Share Posted August 15, 2008 Strange, I've tried using >= and <= but that seems to make no difference. The purpose of the script is basically to measure if the image is too big and if so resize to a certain height and width accordingly and vice versa. Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/#findComment-617546 Share on other sites More sharing options...
Dragoa Posted August 15, 2008 Share Posted August 15, 2008 What do you consider too big though, having a bit of trouble seeing that from the code you've posted, from what I can get this is what your checking for: An image is large if origHeight and origWidth are both over 560(with origHeight being the largest), and the other is if both origHeight and origWidth are both over 540(with origWidth being largest). Although this does leave a dead zone if origHeight and origWidth are between 540 and 560 and origHeight is the largest it'll stay the same. What is the dimension of the image your using for testing? Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/#findComment-617553 Share on other sites More sharing options...
Harley1979 Posted August 15, 2008 Author Share Posted August 15, 2008 I last tested it with an image 2000px x 2100px Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/#findComment-617563 Share on other sites More sharing options...
Dragoa Posted August 15, 2008 Share Posted August 15, 2008 Did a quick test of your if block(occurs to me this should of been the first thing I did...) and it seems to work like it's supposed to(although add in the >=/<= signs, since without them a square image will slip by), and I don't have enough experience(read none) working with the image functions to help see if anything went wrong there. Judging by a quick read over of the imagecopyresample page, my only thought is the decimal places might be messing it up somehow, but I'm doubtful of that. And I can't seem to get the functions(image functions) working on my end to try much else....Sorry. Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/#findComment-617584 Share on other sites More sharing options...
Harley1979 Posted August 15, 2008 Author Share Posted August 15, 2008 Thanks for the help, I'll try your suggestions. The thing is I know this script has worked for me in the past as I have used it on another site which I've refered to for this script I'm using now. Strange, I know I must have missed something or just can't see something but I can't see the wood for the trees after looking at it for days now. Thanks all the same Link to comment https://forums.phpfreaks.com/topic/119865-image-resize-problem/#findComment-617587 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.