3

在 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>';
  }

在这里,我很容易得到图像。但是在这里你可以看到我已经使用了width="100px"height="100px"图像大小。这会扰乱图像纵横比。为了解决这个问题,我在 google 上进行了搜索,发现imagemagick是一个不错的选择。但我不知道如何将 imagemagick 与简单的 php 一起使用(我没有使用任何类、方法)以及如何在这里使用 imagemagick ? 任何帮助和建议都将是非常可观的。谢谢

4

4 回答 4

1

这是保持图像比例的方法

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;
}
于 2013-09-24T10:27:38.507 回答
0
  1. 在 PHP 中使用 HTML 不是一个好习惯,请从 PHP 中删除 HTML
  2. 安装 php imagick

    sudo apt-get install imagemagick
    sudo apt-get install php5-imagick
    
  3. 虽然调整照片大小更好地保持照片的纵横比。以下代码应该更好地了解如何计算纵横比

    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 );
    
     }
    
  4. 请参阅http://php.net/manual/en/book.imagick.php了解如何使用 Imagick。您可以参考以下示例代码

     $image = new Imagick($pathToImage);
     $image->thumbnailImage($newWidth, $newHeight);
     $image->writeImage($pathToNewImage);
    
于 2013-09-24T11:40:06.190 回答
0

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")


?>
于 2013-09-24T10:30:10.640 回答
0

试试 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"
/>';

更多信息:http ://www.sencha.com/learn/how-to-use-src-sencha-io/

于 2013-09-24T10:24:55.523 回答