| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 74174 | 2006-11-13 23:40:00 | php - image resize to specific size but keep aspect ratio -- hard to explain | Morgenmuffel (187) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 498855 | 2006-11-13 23:40:00 | Hi all I am needing to resize images using php I can do that really easily without problem But I have now been faced with a problem I can't figure out I need to resize some images of varying sizes and aspect ratios to fit in a 80 X 80 thumbnail for a flash gallery now the problem is that the thumbnail must be 80 x 80 any other size will get stretched to fit So if my initial image is 800 x 600 and i resize it to 80 X 60 the flash gallery will stretch it to 80 x 80 and the thumbnail is distorted looking, What i need is some sort of function that will ... resize the image correctly to 80 X 60 then add an 80 x 80 background behind the image so that the image will be saved as a correctly proportion image Ok I have explained it badly here is an image (nigel.geek.nz) The one on the left is what is happening the one on the right is what i want |
Morgenmuffel (187) | ||
| 498856 | 2006-11-13 23:50:00 | here is an image (nigel.geek.nz) Cute photo. Ok. Hmmm. I'll try help you if you promise to help my one (kittenwar.com) in the kitten war (http://kittenwar.com/). :D |
Greg (193) | ||
| 498857 | 2006-11-14 00:11:00 | The simplest process I could think of would be to 1) Resize initial image while maintaining ratio, 2) Add appropriate borders dependent on size - see nz2.php.net for possible donor code there. Hope that's a help anyway :) | MyHost (8982) | ||
| 498858 | 2006-11-14 00:15:00 | Apparently this also does the trick - http://phpthumb.sourceforge.net/ "Thumbnails can be a fixed dimension regardless of source aspect ratio and background filled with configurable color. Border corners can optionally be rounded (independant horizontal & vertical radius)" |
MyHost (8982) | ||
| 498859 | 2006-11-14 01:32:00 | Hmm that phpThumb looks good | Morgenmuffel (187) | ||
| 498860 | 2006-11-14 05:24:00 | Hmmm... I recall writing something like what you want. I never actually ended up using it, so its probably full of bugs :p Let me see..... <?php /* $sourceImage = name of the original file that the thumbnail is created from $targetImage = name of the ouput file $forcedWidth = constrains the output file to this width $forcedHeight contrains the output file to this height $keepWHRatio = Keep Width/Height Ratio. With this turned on, you only have to specify either height or width, and the other will resize proportianally. (default = true) I think this is also called Keep Aspect Ration?? If it is, then that would be a better name for the variable. $jpegQuality = quality of the jpeg (0 worst, 100 best, default 75 (recommended)) $deleteOriginal = If you want to delete the original, then input a 1 here. This is for image upload/convert 'on the fly' sort of thing, and it will delete the image uploaded, and replace it with a new image according to your settings. (default = false) */ function resizeimgtojpeg ($sourceImage, $targetImage, $forcedWidth, $forcedHeight, $keepWHRatio = true, $jpegQuality = 75, $deleteOriginal = false) { if(file_exists($sourceImage)) { if ($keepWHRatio == true) { $sourceSize = getimagesize($sourceImage); // For a landscape picture or a square if ($sourceSize[0] >= $sourceSize[1]) { $finalWidth = $forcedWidth; $finalHeight = ($forcedWidth / $sourceSize[0]) * $sourceSize[1]; } // For a potrait picture else { $finalWidth = ($forcedHeight / $sourceSize[1]) * $sourceSize[0]; $finalHeight = $forcedHeight; } } else { $finalWidth = $forcedWidth; $finalHeight = $forcedHeight; } $sourceID = imagecreatefromstring(file_get_contents($sourceIma ge)); $targetID = imagecreatetruecolor($finalWidth, $finalHeight); $target_pic = imagecopyresampled($targetID, $sourceID, 0, 0, 0, 0 , $finalWidth, $finalHeight, $sourceSize[0], $sourceSize[1]); imagejpeg($targetID, $targetImage, $jpegQuality); imagedestroy($targetID); imagedestroy($sourceID); if ($deleteOriginal && file_exists($sourceImage)) { unlink($sourceImage); } return true; } else { return false; } } ?> Don't mock it if its wrong, I am only a learner! |
mejobloggs (264) | ||
| 1 | |||||