Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用image magick和php在从数据库获取图像时调整图像大小_Php_Mysql_Image Processing_Imagemagick - Fatal编程技术网

使用image magick和php在从数据库获取图像时调整图像大小

使用image magick和php在从数据库获取图像时调整图像大小,php,mysql,image-processing,imagemagick,Php,Mysql,Image Processing,Imagemagick,在php中,我使用php move_uploaded_file函数将一个图像上传到数据库。现在,当我从数据库中获取图像时,我使用此代码获取图像 $result = mysql_query("SELECT * FROM "._DB_PREFIX_."storeimages WHERE `city_name`='".$_GET['details']."'"); while($row = mysql_fetch_array($result)){ echo '<div class="store-

在php中,我使用php move_uploaded_file函数将一个图像上传到数据库。现在,当我从数据库中获取图像时,我使用此代码获取图像

$result = mysql_query("SELECT * FROM "._DB_PREFIX_."storeimages WHERE `city_name`='".$_GET['details']."'");
while($row = mysql_fetch_array($result)){
 echo '<div class="store-img">';
  echo '<img class="store-image" src="storeimages/images/'.$row['store_image'].'" width="100px" height="100px" >';
  echo '</div>';
  }
$result=mysql\u query(“从“.\u DB\u PREFIX\u.”中选择*。'storeimages其中'city\u name`='”。$\u GET['details'].“”;
while($row=mysql\u fetch\u数组($result)){
回声';
回声';
回声';
}

在这里,我很容易得到图像。但在这里您可以看到,我使用了
width=“100px”
height=“100px”
作为图像大小。这会干扰图像的纵横比。为了解决这个问题,我在谷歌上搜索了一下,发现这是一个很好的选择。但是我不知道如何在简单的php中使用imagemagick(我没有使用任何类和方法),我在这里如何使用imagemagick?任何帮助和建议都是非常值得的。谢谢

试试Sencha.io。它非常简单和强大

echo '<img
  src="http://src.sencha.io/100/http://yourdomain.com/storeimages/images/'.$row['store_image'].'"
  alt="My constrained image"
  width="100"
  height="100"
/>';
echo';

更多信息:

以下是如何保持图像比例

list($origWidth, $origHeight) = @getimagesize("path/to/image");

$origRatio = $origWidth/$origHeight;
$resultWidth = 100;
$resultHeight = 100;
if ( $resultWidth/$resultHeight > $origRatio ) {
    $resultWidth = $resultHeight * $origRatio;
} else {
    $resultHeight = $resultWidth / $origRatio;
}

imagemagick是一个linux实用程序,通过它可以操作图像

要使用,必须在服务器上安装它

只需键入以下命令

<?
 print_r(exec("which convert"));
?>

如果它返回了一些东西,那么它就被安装了

现在使用以下命令调整图像大小

<?php

exec("/<linux path of this utility>/convert  /<actual path of image>/a.png  -resize 200x200  /<path where image to be saved>/a200x200.png")


?>

  • 在PHP中使用HTML不是一个好的做法,从PHP中删除HTML
  • 安装PHPImagick

    sudo apt-get install imagemagick
    sudo apt-get install php5-imagick
    
  • 调整照片大小时,最好保持纵横比 这张照片的照片。下面的代码应该可以更好地了解如何 计算纵横比

    if( $imageWidth > $maxWidth OR $imageHeight > $maxHeight )
    {
       $widthRatio = 0;
       $heightRatio = 0;
    
       if( $imageWidth > 0 )
       {
           $widthRatio = $maxWidth/$imageWidth;
       }
    
       if( $imageHeight > 0 )
       {
           $heightRatio = $maxHeight/$imageHeight;
       }
    
       if( $widthRatio > $heightRatio )
       {
           $resizeRatio = $heightRatio;
       }
       else
       {
           $resizeRatio = $widthRatio;
       }
    
       $newWidth = intval( $imageWidth * $resizeRatio );
    
       $newHeight = intval( $imageHeight * $resizeRatio );
    
     }
    
  • 请参阅如何使用Imagick。您可以参考以下示例代码

     $image = new Imagick($pathToImage);
     $image->thumbnailImage($newWidth, $newHeight);
     $image->writeImage($pathToNewImage);
    

  • PHP GD比使用imagemagick更简单,大多数主机已经支持它。定义相等的高度和宽度当然会产生一个正方形,因此如果原始图像不是正方形,那么它就不会保留其外观。我建议只使用“高度”属性并删除“宽度”属性-看看这是否能得到您想要的结果(垂直均匀性)