note 111193 rejected from function.imagecreatefromstring by aharvey
| From: | aharvey@php.net | Date: | Fri, 25 Jan 2013 01:15:01 +0000 |
| Subject: | note 111193 rejected from function.imagecreatefromstring by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193381@lists.php.net to get a copy of this message | ||
Note Submitter: irfan_icil at yahoo dot com
----
I have written this php script to get an image from database but before displaying image on browser
i want to resize image according to my specified size ... I don't know what wrong with this
code can anyone help me please i am working on a project have to display an image in usually
passport size image on browser regardless of user had uploaded image of any size
<?php
error_reporting(E_ALL); // report all errors
$mysql_server = "localhost"; //enter mysql server name
$mysql_login = "root"; // enter mysql login name
$mysql_password =""; // enter mysql password
if (!isset($_GET["id"])) { //if id is not given
echo "\n<BR><B><FONT color=\"#FF0000\">Invalid Input
</FONT></B>";
echo "\n<BR><b>Usage:</b> getimage.php?id=<imageid>";
exit();
} else {
$id = (int) $_GET["id"]; // get image id
if ($id == "") {
echo "\n<BR><B><FONT color=\"#FF0000\">Invalid Input
</FONT></B>";
echo "\n<BR><b>Usage:</b> getimage.php?id=<imageid>";
exit();
}
$link = mysql_connect($mysql_server, $mysql_login, $mysql_password); // connect to mysql server
if (!$link) {
die('<BR>Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("tuff"); // select image database
if (!$db_selected) {
die ('<BR>Can\'t use database : ' . mysql_error());
}
$sql = "SELECT image_width,image_height,image_type,image_data";
$sql .= " from image_data where id=$id";
$result = mysql_query($sql);
if (!$result) {
echo "\n<BR>Could not successfully run query ($sql) from database: " .
mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "\n<BR>No data found, nothing to display";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
$type = $row["image_type"]; // get image mime type
$data = $row["image_data"]; //get image data
}
$newSize=150;
$src = imagecreatefromstring($data);
$newwidth = imagesx($src);
$newheight = imagesy($src);
$aspect_ratio = $newheight/$newwidth;
if ($newwidth <= $newsize)
{
$new_w = $newwidth;
$new_h = $newheight;
}
else
{
$new_w = $newsize;
$new_h = abs($new_w * $aspect_ratio);
}
$img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$newwidth,$newheight);
if ($type == "image/jpeg") {
imagejpeg($img);
} else if ($type == "image/x-png") {
imagepng($img);
} else if ($type == "image/gif") {
imagegif($img);
}
//imagedestroy($img);
mysql_free_result($result); // free mysql results
Header( "Content-type:$type"); // sends raw header
echo $src; // sends image data
mysql_close($link); // close link
exit();
}
?>