简体   繁体   English

如何从MySQL数据库获取图像并使用PHP调整其尺寸?

[英]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: 看到:

does it realy need to happen in php? 它真的需要在php中发生吗? there are css properties for that, like max-width 有css属性,例如最大宽度

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标签中显示图像,则可以将widthheight字段添加到img标签中,以所需的尺寸显示图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用imagecopyresampled使用PHP调整图像大小,然后获取内容 - How do I resize an image with PHP using imagecopyresampled and then get the contents 我如何通过php从mysql数据库中获取图像 - How can i get an image from mysql database through php 如何使用PHP从MySQL数据库获取图像 - How to get image from MySQL database using PHP 在搜索中使用PHP,如何在MySql数据库的图像中包含html链接? - Using PHP, within a search, how do I contain an html link in an image from my MySql database? 如何从php中的mysql数据库获取图像 - How to get the an image from mysql database in php 如何从mysql数据库中随机获取变量? (使用php与dbo连接) - How do I randomly get variables from a mysql database? (using php with dbo connection) 如何在不使用PHP的文件处理功能的情况下通过PHP将HTTP内容中的二进制内容上传到MYSQL数据库中? - How do I upload binary content from an HTTP post into a MYSQL database through PHP without using PHP's file handling functions? 如何使用PHP从URL调整图像大小? - How can I resize an image from URL using PHP? 我需要多少字节才能读取图像,以便可以在PHP中获取其尺寸元数据 - how many bytes do I need to read an image so that I can get its dimensions metadata in PHP 使用带有php的image magick在从数据库中提取时调整图像大小 - Using image magick with php to resize an image while fetching from the database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM