EchoFool Posted February 2, 2011 Share Posted February 2, 2011 Is there such a function which allows you pass the image url into a function, it then resizes the image keeping its aspect ratio so that people don't look fat or thin when the picture is in thumbnail form ? Can't see to do it in standard CSS so im wondering if php can some how do it =/ ? Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/ Share on other sites More sharing options...
kenrbnsn Posted February 2, 2011 Share Posted February 2, 2011 I think everyone who uses images has written at least one thumbnail function. Here's one that I did: <?php /* * $fn -- input file * $shrink -- width to shrink picture to if in landscape, height if in portait * $rt -- return image to calling script if "true", send to display if "false" */ function makethumb($fn,$shrink,$rt=true) { list($ow, $oh, $type, $attr) = getimagesize($fn); if ($oh > $ow) { $th = $shrink; $tw = ($th / $oh) * $ow; } if ($ow >= $oh) { $tw = $shrink; $th = ($tw / $ow) * $oh; } $w = round($tw); $h = round($th); $tn = imagecreatetruecolor($w, $h); $img = imagecreatefromjpeg($fn); imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh); if ($rt) { return (array($tn, $type, $w, $h)); } else { header('Content-type: ' .image_type_to_mime_type($type)); imagejpeg($tn); } } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169024 Share on other sites More sharing options...
EchoFool Posted February 2, 2011 Author Share Posted February 2, 2011 It works until i place it in a website then suddenly i just responds with alien characters which fill up the entire page =/ Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169101 Share on other sites More sharing options...
.josh Posted February 2, 2011 Share Posted February 2, 2011 how are you actually using it on your site? Sounds like you are calling the function and then echoing out what it returns (leaving that last argument true). If that's what you are doing, you need to send the proper headers first so that the browser knows its an image to be rendered. Otherwise, the browser is just printing out the raw data of the image. Look at the function, at the code when $rt is set to false. Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169109 Share on other sites More sharing options...
EchoFool Posted February 2, 2011 Author Share Posted February 2, 2011 Function is stored some where then i have: <?php $Url = '[url=http://www.test.com/image.jpg%27;]www.test.com/image.jpg';[/url] echo makethumb($Url,260); ?> I set the value to FALSE also in the function. Is that where im going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169137 Share on other sites More sharing options...
.josh Posted February 3, 2011 Share Posted February 3, 2011 makethumb($Url,260); This send image header and output the image directly. You don't really want to do this unless the only thing being output from your script is the image. $image = makethumb($Url,260,false); This will make the change to the image and return the altered image resource. You will want to do it like this if you are wanting to do something more with the image and then output it later, or save it to disk instead or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169143 Share on other sites More sharing options...
EchoFool Posted February 3, 2011 Author Share Posted February 3, 2011 I tried both methods - but get this in both circumstances : Before the first part i get: Warning: Cannot modify header information - headers already sent by (output started at main.php:85) in functions.php on line 548 Obviously its unreadable http://www.paste.to/ODA4Mw== im assuming this is raw image data? <?php function makethumb($fn,$shrink,$rt=false) { list($ow, $oh, $type, $attr) = getimagesize($fn); if ($oh > $ow) { $th = $shrink; $tw = ($th / $oh) * $ow; } if ($ow >= $oh) { $tw = $shrink; $th = ($tw / $ow) * $oh; } $w = round($tw); $h = round($th); $tn = imagecreatetruecolor($w, $h); $img = imagecreatefromjpeg($fn); imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh); if ($rt) { return (array($tn, $type, $w, $h)); } else { header('Content-type: ' .image_type_to_mime_type($type)); imagejpeg($tn); } } $Url = 'http://www.google.co.uk/images/logos/ps_logo2.png'; //test image off the net $image = makethumb($Url,260,false); echo $image; ?> Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169150 Share on other sites More sharing options...
.josh Posted February 3, 2011 Share Posted February 3, 2011 Yes, that is raw image data. And you are getting that error because you are outputting something before it. See the sticky about headers. Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169156 Share on other sites More sharing options...
.josh Posted February 3, 2011 Share Posted February 3, 2011 also, you can't just echo $image. It's an image source object. If you want to output it to browser, you have to do it like this: header('Content-type: ' .image_type_to_mime_type($type)); imagejpeg($tn); // for jpg files, there's other image type equivalents to this Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169159 Share on other sites More sharing options...
EchoFool Posted February 3, 2011 Author Share Posted February 3, 2011 oh that might be why then - i'll set it to true and display the array values where necessary then. Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169160 Share on other sites More sharing options...
EchoFool Posted February 3, 2011 Author Share Posted February 3, 2011 also, you can't just echo $image. It's an image source object. If you want to output it to browser, you have to do it like this: header('Content-type: ' .image_type_to_mime_type($type)); imagejpeg($tn); // for jpg files, there's other image type equivalents to this would image.$type($tn); work or is the functions not named equal to the file type? With the header line its always going to error if its the middle of script and i can't put the header else where it won't know the $type without the variable which is half way through the script so theres no logic to it. Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169164 Share on other sites More sharing options...
.josh Posted February 3, 2011 Share Posted February 3, 2011 Well they are but...you can't just call a function like that....if you really want to do it like that then you would have to eval it, but that's not really a good idea. Instead, you should setup a condition to output based on type of image. $ext = strtolower(pathinfo($Url, PATHINFO_EXTENSION)); switch($ext) { case 'jpg' : case 'jpeg' : imagejpeg($tn); break; case 'png' : imagepng($tn); break; case 'gif' : imagegif($tn); break; case 'bmp' : imagewbmp($tn); break; } Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169176 Share on other sites More sharing options...
.josh Posted February 3, 2011 Share Posted February 3, 2011 With the header line its always going to error if its the middle of script and i can't put the header else where it won't know the $type without the variable which is half way through the script so theres no logic to it. You need to a) save the image instead of output and then point to it like any other image (in an <img .../> tag) b) seeing as how option "a" isn't all that great, an alternative is to make the image generation script a completely separate script all by itself. You would then output your image tag in your script as you normally output an image tag, except point to the image script like so: <img src='image.php?url=urlhere&shrink=260' ... /> Then in script.php is where you have that function and the only thing it will do is the arguments from $_GET['url'] and $_GET['shrink'] and then output the image. Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169179 Share on other sites More sharing options...
EchoFool Posted February 3, 2011 Author Share Posted February 3, 2011 Genius that works Thanks much Quote Link to comment https://forums.phpfreaks.com/topic/226490-image-aspect-ratio/#findComment-1169196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.