Guest Posted January 10, 2007 Share Posted January 10, 2007 Im am storing my images in a mysql database and need to pull it out of the database and resize it to a correct ratio size for height and width. Is there any way that i can do this since i cant use getimagesize is there any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/ Share on other sites More sharing options...
Jessica Posted January 10, 2007 Share Posted January 10, 2007 Why can't you? Create the image and then use the GD library. Why won't this work? Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-157007 Share on other sites More sharing options...
Accipiter Posted January 10, 2007 Share Posted January 10, 2007 And to suggest which functions to use with the data you pulled from your database:[url=http://se2.php.net/function.imagecreatefromstring]http://se2.php.net/function.imagecreatefromstring[/url]The function will autodetect what type (jpg/gif/png/etc.) of image it is in your variable and create it. Then you can just manipulate it with the other GD functions.With the functions[url=http://se2.php.net/manual/en/function.imagesx.php]http://se2.php.net/manual/en/function.imagesx.php[/url][url=http://se2.php.net/manual/en/function.imagesy.php]http://se2.php.net/manual/en/function.imagesy.php[/url]you can retrieve the image width and height.Good luck! :) Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-157194 Share on other sites More sharing options...
Guest Posted January 10, 2007 Share Posted January 10, 2007 I cant quite get it working.[code] // Load image $img = $row[$imgnum]; if($mwidth != "" && $mheight != "") { // Get image size and scale ratio $width = imagesx($img); $height = imagesy($img); $scale = min($mwidth/$width, $mheight/$height); // If the image is larger than the max shrink it if ($scale < 1) { $new_width = floor($scale*$width); $new_height = floor($scale*$height); // Create a new temporary image $tmp_img = imagecreatetruecolor($new_width, $new_height); // Copy and resize old image into new image imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($img); $img = $tmp_img; } header("Content-type: $imagetype"); echo $img; } else { // set the header for the image header("Content-type: $imagetype"); echo $img; }[/code]but the results are The image “http://www.bestorlandorealty.com/inc/view.php?ID=5&type=house&image=1&wid=100&hei=100” cannot be displayed, because it contains errors.The image is fine if i just display it I dont know whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-157721 Share on other sites More sharing options...
Accipiter Posted January 11, 2007 Share Posted January 11, 2007 Well, I think I know what is wrong.I assume you've stored the image file (jpg, gif, png or so) in the database.Then, you must load the image into the GD. The image resource (which should be $img) and the image file ($row[$imgnum]) is in VERY different format. Try like this instead.[code] if($mwidth != "" && $mheight != "") { // Load image to GD from the data $img = imagecreatefromstring($row[$imgnum]); // Get image size and scale ratio $width = imagesx($img); $height = imagesy($img); $scale = min($mwidth/$width, $mheight/$height); // If the image is larger than the max shrink it if ($scale < 1) { $new_width = floor($scale*$width); $new_height = floor($scale*$height); // Create a new temporary image $tmp_img = imagecreatetruecolor($new_width, $new_height); // Copy and resize old image into new image imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($img); $img = $tmp_img; } // If you really wish for the image to be of the same format as $imagetype, // then you can make a switch-case here. Now I set it so send the thumb as jpeg header("Content-type: image/jpeg"); imagejpeg($img); } else { // set the header for the image header("Content-type: $imagetype"); echo $row[$imgnum]; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-158243 Share on other sites More sharing options...
Accipiter Posted January 11, 2007 Share Posted January 11, 2007 (Ah, just as an addition)Ofcourse, as I wrote it now, it will load the image into the GD and then send it as a JPEG no matter if it was scaled down or not, as long as a width and height was given. My suggestion was just to give you an idea how GD works.Hope it helps you along the way! Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-158247 Share on other sites More sharing options...
Guest Posted January 11, 2007 Share Posted January 11, 2007 Thank you so much i figured it out from the code that you gave me. Thank you once again. Quote Link to comment https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-158550 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.