lional Posted August 31, 2008 Share Posted August 31, 2008 Hi All I am writing an application where one page uploads an image to my webserver. I would like the next page to display that image but making it smaller to fit on the page but I would still like the image to be in proportion. Is this poissible in PHP Thanks Lional Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/ Share on other sites More sharing options...
ratcateme Posted August 31, 2008 Share Posted August 31, 2008 have a look at this http://nz.php.net/manual/en/function.imagecopyresized.php look at the first example you can calculate the percentage. then run that code and it will downsize your image Scott. Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-630271 Share on other sites More sharing options...
lional Posted August 31, 2008 Author Share Posted August 31, 2008 Is there a way to get the dimensions (width and length) of the file we are uploading. The image that gets uploaded will differ depending on the image that the user uploads. I know how to get the file size in Kb or Mb. Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-630275 Share on other sites More sharing options...
JasonLewis Posted August 31, 2008 Share Posted August 31, 2008 Have a look at getimagesize() to get the width and height of an image. To resize an image that is greater then a set width or height you can use this: <?php list($width, $height) = getimagesize("image.jpg"); $maxWidth = 300; $maxHeight = 500; //First check if the image has greater width or height then the maximums if($width > $maxWidth || $height > $maxHeight){ //If you haven't specified a max width because you are only constraining the height, then we set the widthDivision as the width of the image. if($maxWidth == ""){ $widthDivision = $width; }else{ //If maxWidth is specified, we set the width division as the maxWidth divide by the width of the image. $widthDivision = $maxWidth/$width; } //Same for height as was said in the width. if($maxHeight == ""){ $heightDivision = $height; }else{ $heightDivision = $maxHeight/$height; } //Now the brains, the min function. Get the lowest number out of the width and height. $MIN = min($widthDivision, $heightDivision); //Now we can set the new widths and height by multiplying the lowest number into both the original width and height. This will constrain the proportions... $newWidth = $MIN*$width; $newHeight = $MIN*$height; }else{ //If the image is fine, just set the new width and height as the default. $newWidth = $width; $newHeight = $height; } ?> Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-630283 Share on other sites More sharing options...
lional Posted September 1, 2008 Author Share Posted September 1, 2008 The image seems to depixulate Any Ideas Thanks Lional Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631072 Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 http://nz.php.net/manual/en/function.imagecopyresampled.php This is a far better function - I never use imageCopyRezised for just this reason. Resampling does take more memory though... Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631235 Share on other sites More sharing options...
lional Posted September 2, 2008 Author Share Posted September 2, 2008 I have tried using this but I get "garbage" printing out instead of my image Here is my code $effects_image = $_SESSION['photo']; list($width, $height) = getimagesize("$effects_image"); $maxWidth = 256; $maxHeight = 256; if ($width > $maxwidth || $height > $maxheight) { //If you haven't specified a max width because you are only constraining the height, then we set the widthDivision as the width of the image. if($maxWidth == ""){ $widthDivision = $width; }else{ //If maxWidth is specified, we set the width division as the maxWidth divide by the width of the image. $widthDivision = $maxWidth/$width; } //Same for height as was said in the width. if($maxHeight == ""){ $heightDivision = $height; }else{ $heightDivision = $maxHeight/$height; } //Now the brains, the min function. Get the lowest number out of the width and height. $MIN = min($widthDivision, $heightDivision); //Now we can set the new widths and height by multiplying the lowest number into both the original width and height. This will constrain the proportions... $newWidth = $MIN*$width; $newHeight = $MIN*$height; }else{ //If the image is fine, just set the new width and height as the default. $newWidth = $width; $newHeight = $height; } // Resample $image_p = imagecreatetruecolor($newWidth, $newHeight); $image = imagecreatefromjpeg($effects_image); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Output imagejpeg($image_p, null, 100); Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631649 Share on other sites More sharing options...
ratcateme Posted September 2, 2008 Share Posted September 2, 2008 add this somewhere header('Content-Type: image/jpeg'); Scott. Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631654 Share on other sites More sharing options...
lional Posted September 2, 2008 Author Share Posted September 2, 2008 It has been added to the top of my page, still have the same problem Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631672 Share on other sites More sharing options...
beedie Posted September 2, 2008 Share Posted September 2, 2008 there mustn't be any content at all before the header('Content-Type: image/jpeg'); if there needs to be which is likely you will need to use ob_start() and ob_flush() . Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631686 Share on other sites More sharing options...
lional Posted September 2, 2008 Author Share Posted September 2, 2008 I have this code on the top of my page ob_flush();ob_start(); header('Content-type: image/jpeg'); session_start(); Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631688 Share on other sites More sharing options...
ratcateme Posted September 3, 2008 Share Posted September 3, 2008 what is the buffering for?? Scott. Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-632454 Share on other sites More sharing options...
lional Posted September 3, 2008 Author Share Posted September 3, 2008 I was going on what beedie said because I just get garbage on the screen it is not displaying the image and I don't know why. Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-632460 Share on other sites More sharing options...
ratcateme Posted September 3, 2008 Share Posted September 3, 2008 i have never found that i need that but i would recommend placing it on the line above imagejpeg($image_p, null, 100); Scott. Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-632484 Share on other sites More sharing options...
beedie Posted September 7, 2008 Share Posted September 7, 2008 Did you get it sorted? <?php ///your image generation code ending with(change the file name to the file name and path you want): imagejpeg($image_p, savepath/image_name.jpg, 100); echo '<img src="savepath/image_name.jpg" />'; //that create the image tag and you needn't worry about the headers, sessions, output bufferring etc. ?> Link to comment https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-636039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.