UPDATE: For anyone else wondering about this, check out the comment at http://www.php.net/manual/en/function.imagecreatefromstring.php#31178
I have images stored in a database table. They’re uploaded in their original dimensions.
I can output this image to browser using PHP headers no problem (like “getimage.php?imgID=1”), but I’m stuck if I want to resize the image before it’s output to the browser.
I do not want to write to a new file, I just want to get an image from the database table, and resize it (preferably in the same PHP script).
Something like this would be perfect: getimage.php?imgID=1&width=75&height=75
Can anyone tell me how I could do this in PHP?
$query= "select * from `tablename`.`photos` where `ImageID` = '".$_GET['imgID']."' LIMIT 1";
$downloadResult = SendQuery("browse",$query); // my own custom php function to connect & send a mysql query
if(is_array($downloadResult)){ //check if a row was returned
foreach($downloadResult as $myfile) {
header("Content-Type: ". $myfile['mime']);
header("Content-Length: ". $myfile['size']);
if($d=="true"){
header('Content-Disposition: inline; filename="'. urlencode($myfile['name']).'"');
}
echo $myfile['data'];
}
You load the image data from a binary string (
$myfile['data']). All you need is an image library/extension that is able to create an image object based on a binary string. For example the GD library can load data from strings:You can then take any of the very many examples (including tons of them on this site already) to resize your image to your needs.
After you have resized your image, you can output it to the browser, e.g this for a PNG:
See
imagecreatefromstringDocs. You might find it helpful if you use a image library that does resizing with an easy interface, for example Wideimage which supports loading from a binary string as well. It’s GD based:Yes, it’s that simple. See: