note 31178 added to function.imagecreatefromstring
| From: | shiehj at yahoo dot com | Date: | Sat, 12 Apr 2003 02:44:26 +0000 |
| Subject: | note 31178 added to function.imagecreatefromstring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46533@lists.php.net to get a copy of this message | ||
Here is the code I did to create a thumbnail image from the database blob field. The trick is to use
"imagecreatefromstring()" to create an image file.
Jack Shieh
<?php
require("dbconfig.inc");
$id = $_GET['id'];
if($id) {
$link = @mysql_connect($host, $user, $password) or die("Could not connect: " .
mysql_error());
@mysql_select_db($dbname, $link);
$query = "select filetype, image from pictures where id = $id";
$result = @mysql_query($query);
$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);
};
?>
--
http://www.php.net/manual/en/function.imagecreatefromstring.php
http://master.php.net/manage/user-notes.php?action=edit+31178
http://master.php.net/manage/user-notes.php?action=delete+31178
http://master.php.net/manage/user-notes.php?action=reject+31178