2013-09-24 21 views
3

在php中,我使用php move_uploaded_file函数将图像上载到数据库。现在,当我从数据库中获取图像,我用这个代码来获取图像使用图像magick与php来从数据库中提取图像时调整图像的大小

$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"作为图像大小。这是令人不安的图像宽高比。为了解决这个问题,我搜索了谷歌,我得到了imagemagick是一个很好的选择。但我不知道如何使用imagemagick与简单的PHP(我没有使用任何类,方法),我怎样才能在这里使用imagemagick ?任何帮助和建议都将非常可观。谢谢

+0

PHP GD比使用ImageMagick简单,大多数托管服务已经支持它。 – ihsan

+0

定义相同的高度和宽度当然会产生一个正方形,所以如果你的原始图像不是正方形,那么它将不会保留其方面。我会建议只使用高度属性并删除宽度属性 - 看看这是否给你你正在寻找的结果(垂直一致性) – verbumSapienti

回答

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


?> 
0
  1. 它不是一个很好的做法,中使用HTML PHP,从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);