Mutley Posted March 24, 2007 Share Posted March 24, 2007 Instead of fixing the images height/width, can I have it show by a ratio of the original size? What I have is a large image and want to make a small "preview" of it (without creating another file), at the moment I just set it to 80x80pixels but the problem with this is if the large image isn't square it looks squashed. Is it possible to do it by a ratio? Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/ Share on other sites More sharing options...
AndyB Posted March 24, 2007 Share Posted March 24, 2007 It's really not a good idea to just change image dimensions since the full size image will have to load anyway while the visitor waits ... and waits. Ratioing is simple. Use getimagesize() to determine the x and y dimensions, then a little basic math and you're done. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214434 Share on other sites More sharing options...
Lumio Posted March 24, 2007 Share Posted March 24, 2007 try that function: <?php function resizeimage($file){ if(!file_exists($filename)) return false; $im_info = getimagesize($filename); //unresized image $im_width = $im_info[0]; $im_height = $im_info[1]; $im_flag = $im_info[2]; if ($im_flag == 1) $image = imagecreatefromgif($filename); //unresized image elseif ($im_flag == 2) $image = imagecreatefromjpeg($filename); elseif ($im_flag == 3) $image = imagecreatefrompng($filename); else return false; //max height: 100px; max width: 100px if($im_width >= $im_height) { $im_divice = $im_width / 100; }else { $im_divice = $im_height / 100; } $thumb_width = $im_width / $im_divice; $thumb_height = $im_height / $im_divice; //create empty image $thumb = imagecreatetruecolor($thumb_width, $thumb_height); //resize image imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height); header('Content-Type: image/jpeg', true); imagejpeg($thumb) imagedestroy($thumb); //destroy temporary image-resource imagedestroy($image); //destroy temporary image-resource } ?> Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214437 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 How do I use that function? Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214560 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 I tried: resizeimage("images/products/$image"); But just got errors. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214807 Share on other sites More sharing options...
Lumio Posted March 25, 2007 Share Posted March 25, 2007 witch errors? try resizeimage("./images/products/$image"); Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214814 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 Error: Parse error: syntax error, unexpected T_STRING on line 291 .... 289: imagejpeg($thumb) 290: 291: imagedestroy($thumb); //destroy temporary image-resource 292: imagedestroy($image); //destroy temporary image-resource Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214888 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 Fixed it, sorry is mistake, although now I get this: Fatal error: Cannot redeclare resizeimage() (previously declared in products.php:262) Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214893 Share on other sites More sharing options...
AndyB Posted March 25, 2007 Share Posted March 25, 2007 The error message says it all. I'd suspect that your code has attempted to re-declare the same function. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214926 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 All I've done is this: <? resizeimage("./images/products/$id.jpg");?> Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214928 Share on other sites More sharing options...
AndyB Posted March 25, 2007 Share Posted March 25, 2007 Are you sure you haven't got the function definition in an included file as well as inline, i.e. it's being declared twice. The error message is not about how you use the function, it's about how many times you have declared it. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214930 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 I have only declared it once, I have another function but it's called something different. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214932 Share on other sites More sharing options...
AndyB Posted March 25, 2007 Share Posted March 25, 2007 How strange. You only declare it once and it's not an intrinsic function so I wonder why php gives you that error message? Just for laughs, change <? to <?php and rename the resize function to something like x_resizeimage. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214939 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 Nope, it says it's redeclared on line 262 and on line 262 is the function I'm using. I think it's to do with the: <?php resizeimage("./images/products/$id.jpg");?> But isn't that USING the function not declaring it? Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214947 Share on other sites More sharing options...
Mutley Posted March 25, 2007 Author Share Posted March 25, 2007 I fixed it, I put the function at the very top of my file. However no image is displaying from using it ? What exactly do I need to do ? I have: <?php resizeimage("./images/products/$id.jpg");?> $id is the file name, it doesn't show any image or code in the HTML though. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-214964 Share on other sites More sharing options...
AndyB Posted March 25, 2007 Share Posted March 25, 2007 Do you get any php errors displayed? If so, what? Are you certain that the image exists? Are you certain that the path to the image is appropriate for the function? Add a line like: <?php echo "<img src='./images/products/". $id. ".jpg'>"; ?> What do you see? (I might believe ../images/products/ as the path) Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215036 Share on other sites More sharing options...
Mutley Posted March 26, 2007 Author Share Posted March 26, 2007 The image shows when I do the echo but the function still doesn't do anything, this is what I'm trying: <?php function resizeimage($file){ if(!file_exists($filename)) return false; $im_info = getimagesize($filename); //unresized image $im_width = $im_info[0]; $im_height = $im_info[1]; $im_flag = $im_info[2]; if ($im_flag == 1) $image = imagecreatefromgif($filename); //unresized image elseif ($im_flag == 2) $image = imagecreatefromjpeg($filename); elseif ($im_flag == 3) $image = imagecreatefrompng($filename); else return false; //max height: 100px; max width: 100px if($im_width >= $im_height) { $im_divice = $im_width / 100; }else { $im_divice = $im_height / 100; } $thumb_width = $im_width / $im_divice; $thumb_height = $im_height / $im_divice; //create empty image $thumb = imagecreatetruecolor($thumb_width, $thumb_height); //resize image imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height); header('Content-Type: image/jpeg', true); imagejpeg($thumb); imagedestroy($thumb); //destroy temporary image-resource imagedestroy($image); //destroy temporary image-resource } $id = '44'; echo "<img src='./images/products/". $id. ".jpg'>"; resizeimage(".images/products/". $id .""); resizeimage(".images/products/". $id .".jpg"); ?> Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215057 Share on other sites More sharing options...
Lumio Posted March 26, 2007 Share Posted March 26, 2007 Sorry there are some mistakes of mine: <?php function resizeimage($filename){ //here was my mistake if(!file_exists($filename)) return false; $im_info = getimagesize($filename); //unresized image $im_width = $im_info[0]; $im_height = $im_info[1]; $im_flag = $im_info[2]; if ($im_flag == 1) $image = imagecreatefromgif($filename); //unresized image elseif ($im_flag == 2) $image = imagecreatefromjpeg($filename); elseif ($im_flag == 3) $image = imagecreatefrompng($filename); else return false; //max height: 100px; max width: 100px if($im_width >= $im_height) { $im_divice = $im_width / 100; }else { $im_divice = $im_height / 100; } $thumb_width = $im_width / $im_divice; $thumb_height = $im_height / $im_divice; //create empty image $thumb = imagecreatetruecolor($thumb_width, $thumb_height); //resize image imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height); header('Content-Type: image/jpeg', true); imagejpeg($thumb, NULL, 100); //thumbnail with the best quality imagedestroy($thumb); //destroy temporary image-resource imagedestroy($image); //destroy temporary image-resource } $id = '44'; //no echos please //echo "<img src='./images/products/". $id. ".jpg'>"; //also you don't use that //resizeimage(".images/products/". $id .""); //the filename was wrong... it has to be ./... resizeimage("./images/products/". $id .".jpg"); ?> If your php-script is in /Users/ and also your picture is in /Users/ you say ./image.jpg the only . stands for the same directory as the directory of your php-script. if you say ../image.jpg it tries to read the file image.jpg of / Do you understand? And if you write .image.jpg then the file is called .image.jpg Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215171 Share on other sites More sharing options...
Mutley Posted March 26, 2007 Author Share Posted March 26, 2007 I get an evil headers cannot be edited now, although the test file works! How do I take this error off? Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215212 Share on other sites More sharing options...
Lumio Posted March 26, 2007 Share Posted March 26, 2007 remove all echos Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215213 Share on other sites More sharing options...
Mutley Posted March 27, 2007 Author Share Posted March 27, 2007 Can't get rid of the error. I tried removing the HTML <header>, deleting all spaces and it keeps pop-uping up, even saying the headers are sent in the config file (which only has DB variables in! There are no echos when I use the function. Not seeming to work. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215785 Share on other sites More sharing options...
Mutley Posted March 27, 2007 Author Share Posted March 27, 2007 I tried ob_start above the function and moving the function about, I also checked for white gaps but found none. No luck at all. Keeps saying header is sent. Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215799 Share on other sites More sharing options...
Lumio Posted March 27, 2007 Share Posted March 27, 2007 Post code and pageerrors Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-215870 Share on other sites More sharing options...
ted_chou12 Posted March 27, 2007 Share Posted March 27, 2007 I use getimagesize() to get the original width height of the image, and by using the while(), divide both width and height at the same time until it goes below the max, like while($image[0] > 100 or $image[1] > 100) {... divide by 2 or something} this keeps the ratio of the width to height meanwhile, you also have full and easy control over the image size. Ted Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-216019 Share on other sites More sharing options...
jitesh Posted March 27, 2007 Share Posted March 27, 2007 Search a with a keyword 'image resize' in http://www.phpclasses.org/ Link to comment https://forums.phpfreaks.com/topic/44162-displaying-images-in-a-ratio/#findComment-216031 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.