[英]How do I get an image from a mySQL database and resize it's dimensions using PHP?
UPDATE: For anyone else wondering about this, check out the comment at http://www.php.net/manual/en/function.imagecreatefromstring.php#31178 更新:对于对此有任何疑问的其他人,请查看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.
我可以毫无问题地使用PHP标头将此图像输出到浏览器(例如“ getimage.php?imgID = 1”),但是如果要在将图像输出到浏览器之前对其进行调整,则会遇到麻烦。
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). 我不想写入新文件,我只想从数据库表中获取图像,并调整其大小(最好在同一PHP脚本中)。
Something like this would be perfect: getimage.php?imgID=1&width=75&height=75 像这样的东西将是完美的:getimage.php?imgID = 1&width = 75&height = 75
Can anyone tell me how I could do this in PHP? 谁能告诉我如何用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']
). 您从二进制字符串(
$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:
例如, GD库可以从字符串加载数据:
$im = imagecreatefromstring($myfile['data']);
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, eg this for a PNG: 调整图像大小后,可以将其输出到浏览器,例如PNG:
imagepng($im);
imagedestroy($im);
See imagecreatefromstring
Docs . 请参阅
imagecreatefromstring
文档 。 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. 如果使用的图像库使用简单的界面来调整大小,则可能会发现它很有用,例如Wideimage也支持从二进制字符串加载。 It's GD based:
基于GD:
WideImage::loadFromString($myfile['data'])
->resize(50, 30)
->output('png');
Yes, it's that simple. 是的,就是这么简单。 See:
看到:
Please provides image re size using extension named - imagic 请使用扩展名为-imagic的图像重新调整大小
Other useful script :- 其他有用的脚本:-
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/ http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
http://shiftingpixel.com/2008/03/03/smart-image-resizer/ http://shiftingpixel.com/2008/03/03/smart-image-resizer/
you can use imagemagick, like this (untested code): 您可以使用imagemagick,如下所示(未经测试的代码):
//your image code here
$image_path = 'path-to-image';
$width = null;
$height = null;
if(array_key_exists('width', $_REQUEST)) {
$width = intval($_REQUEST['width']);
}
if(array_key_exists('height', $_REQUEST)) {
$height = intval($_REQUEST['height']);
}
//add your header infos like
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename="filename.jpg"');
if(null !== $width && null !== $height) {
$cmd = 'convert '.escapeshellarg($image_path).' -resize '.$width.'x'.$height.' - ';
echo shell_exec($cmd);
}
else {
readfile($image_path);
}
What does your getimage.php script do at the moment? 您的getimage.php脚本目前做什么? Does it display the image from the database in a
HTML
img
tag? 它是否以
HTML
img
标签显示数据库中的img
? If it displays the image in an img
tag then you could add the fields for width
and height
to the img
tag to display the image at the size you require. 如果它在
img
标签中显示图像,则可以将width
和height
字段添加到img
标签中,以所需的尺寸显示图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.