cooldude832 Posted August 15, 2007 Share Posted August 15, 2007 I have images stored in a mysql table as binary and i reproduce them with this script: <?php require_once("functions.php"); // just so we know it is broken error_reporting(E_ALL); // some basic sanity checks if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) { //connect to the db connectSQL(); // get the image from the db $sql = "SELECT image FROM Images WHERE ImageID=".$_GET['image_id']; // the result of the query $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); // set the header for the image header("Content-type: image/jpeg"); echo mysql_result($result, 0); // close the db link mysql_close($link); } else { echo 'Please use a real id number'; } ?> My question is how can i resize that (i.e make it the image resource for GD) thats my own issue is figuring out how to link it as a resource in gd if its a temp image. Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/ Share on other sites More sharing options...
keeB Posted August 15, 2007 Share Posted August 15, 2007 Here's the code I use to resize an image by scale: <?php public function ResizeByScale($scale, $outputFile) { /** * @param float scale: The scale at which you want to resize the image * @return: void * * @output: Writes $outputFile to folder. * */ $width = $this->getWidth(); $height = $this->getHeight(); $newWidth = $width * $scale; $newHeight = $height * $scale; $image_p = ImageCreateTrueColor($newWidth, $newHeight); $image = ImageCreateFromJPEG($this->getPath()); ImageCopyResampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagePNG($image_p, $outputFile, $this->getImageQuality()); ?> The thing you'll obviously have to modify is $width, $height, and the ImageCreateFromJPEG($this->getPath) <--- you can use your binary data for this I believe. Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-324367 Share on other sites More sharing options...
cooldude832 Posted August 15, 2007 Author Share Posted August 15, 2007 yeah i understand that but can I use the binary as my image resource? Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-324369 Share on other sites More sharing options...
keeB Posted August 15, 2007 Share Posted August 15, 2007 Yeah I am pretty sure you can. Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-324373 Share on other sites More sharing options...
Barand Posted August 15, 2007 Share Posted August 15, 2007 I've never used this, as I don't use blob images, but maybe imagecreatefromstring() Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-324455 Share on other sites More sharing options...
cooldude832 Posted August 15, 2007 Author Share Posted August 15, 2007 I'll look into it, but if i create the iamge first what would I use for my handler/how would i do that Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-325200 Share on other sites More sharing options...
cooldude832 Posted August 15, 2007 Author Share Posted August 15, 2007 I found this on php.net and i think it will suite my needs just fine <?php $data = @mysql_result($result,0,"image"); $type = @mysql_result($result,0,"filetype"); Header( "Content-type: $type"); $size = 150; // new image width $src = imagecreatefromstring($data); $width = imagesx($src); $height = imagesy($src); $aspect_ratio = $height/$width; if ($width <= $size) { $new_w = $width; $new_h = $height; } else { $new_w = $size; $new_h = abs($new_w * $aspect_ratio); } $img = imagecreatetruecolor($new_w,$new_h); imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height); // determine image type and send it to the client if ($type == "image/pjpeg") { imagejpeg($img); } else if ($type == "image/x-png") { imagepng($img); } else if ($type == "image/gif") { imagegif($img); } imagedestroy($img); mysql_close($link); }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-325208 Share on other sites More sharing options...
cooldude832 Posted August 15, 2007 Author Share Posted August 15, 2007 I've got this version working now, but I want to know how can I resize based on height? <?php require_once("functions.php"); $id = $_GET['image_id']; if(!empty($_GET['width'])){ $size = $_GET['width']; } else{ $size = 500; } if($id) { connectSQL(); $query = "select Image, ImageType from Images where ImageID = $id"; $result = mysql_query($query) or die(mysql_error()); $data = mysql_result($result,0,"Image"); $type = mysql_result($result,0,"ImageType"); $type = "image/".$type; Header("Content-type: ".$type); $src = imagecreatefromstring($data); $width = imagesx($src); $height = imagesy($src); $aspect_ratio = $height/$width; $new_w = $size; $new_h = abs($new_w * $aspect_ratio); $img = imagecreatetruecolor($new_w,$new_h); imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height); // determine image type and send it to the client if ($type == "image/pjpeg" || $type == "image/jpg" || type == "image/jpeg") { imagejpeg($img); } else if ($type == "image/x-png" || $type = "image/png") { imagepng($img); } else if ($type == "image/gif") { imagegif($img); } imagedestroy($img); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-325233 Share on other sites More sharing options...
Barand Posted August 15, 2007 Share Posted August 15, 2007 Decide the height you want. Get the height it is now. Scale the width by the same ratio. Compared with partial differential equations, it's child's play Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-325249 Share on other sites More sharing options...
cooldude832 Posted August 15, 2007 Author Share Posted August 15, 2007 yeah, but I'm thinking its a bad idea because browsers have a set width (I don't like side scrolls) and an infinte height so i'll just size in the height and if it doesn't fit my height needs I'll say scale it down more using a loop till i get numbers that manage. The width will be less than the desired, but it will suite my needs. Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-325253 Share on other sites More sharing options...
elkidogz Posted August 16, 2007 Share Posted August 16, 2007 set either the width or the height as a standard (like all pics new height is 200) then do some math to work out what the width of the pic is, make a new pic from the old pic with the new height and new width and then put it into a folder. Quote Link to comment https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/#findComment-325328 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.