图像调整大小与PHP?

时间:2011-09-26 07:01:54

标签: php image resize

我已经在网上完成了我的研究,在这里我也不断提出任何可以帮助我的答案: 我有以下代码,根据用户的位置显示文件夹中的图像,但图像太大,我需要调整大小。 我尝试或阅读过的所有脚本都与正在上传的文件有关。任何人都可以把我推向正确的方向吗?

谢谢你。

 <?php


        print"  

        <table  <td width=\"138\" height=\"73\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
      <tr>
        <td align=\"center\" valign=\"middle\"><a href=\"map.php\"><img src=\"" .
         esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') .
         "\" alt=\"Map of systems around {$user['location']}\" /></a></td>
      </tr>
    </table>
         "
    ?>

我的问题源于我需要将图像拉为:

<img src=\"" .esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') . "\" alt=\"Map of systems around {$user['location']}\" /></a>

4 个答案:

答案 0 :(得分:2)

前一段时间写了tutorial about this。也许它可以帮助。它从上传开始,但大部分都是关于调整大小。只需通过不同的方式设置图像类型和文件名来换掉$ _FILES数组的用法。这是您需要的代码:

// Create image from file
switch(strtolower($_FILES['image']['type']))
{
     case 'image/jpeg':
         $image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
         break;
     case 'image/png':
         $image = imagecreatefrompng($_FILES['image']['tmp_name']);
         break;
     case 'image/gif':
         $image = imagecreatefromgif($_FILES['image']['tmp_name']);
         break;
     default:
         exit('Unsupported type: '.$_FILES['image']['type']);
}

// Target dimensions
$max_width = 240;
$max_height = 180;

// Get current dimensions
$old_width  = imagesx($image);
$old_height = imagesy($image);

// Calculate the scaling we need to do to fit the image inside our frame
$scale      = min($max_width/$old_width, $max_height/$old_height);

// Get the new dimensions
$new_width  = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);

// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);

// Resize old image into new
imagecopyresampled($new, $image, 
     0, 0, 0, 0, 
     $new_width, $new_height, $old_width, $old_height);

// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$data = ob_get_clean();

// Destroy resources
imagedestroy($image);
imagedestroy($new);

// Set new content-type and status code
header("Content-type: image/jpeg", true, 200);

// Output data
echo $data;

如果要将图像存储为文件而不是将其转储到浏览器,请删除末尾的head和echo部分,然后在NULL调用中换出imagejpeg参数。实际的文件名。希望有所帮助:)

以下是使用中的代码:http://samples.geekality.net/image-resize/

答案 1 :(得分:1)

你看看gd:http://www.php.net/manual/en/function.imagecopyresized.php

你可以试试这个:

$extension = substr( $img_url, -3 );
$extension = strtolower($extension);
switch ($extension) {
case "jpg":
case "jpeg":
$src_im = createimagefromjpeg($img_url);
break;
case "gif":
$src_im = createimagefromgif($img_url);
break;
case "png":
$src_im = createimagefrompng($img_url);
break;
}
// Get size
$size = GetImageSize($img_url);
$src_w = $size[0];
$src_h = $size[1];

// $width has to be fixed to your wanted width
$dst_w = $width;
$dst_h = round(($dst_w / $src_w) * $src_h);
$dst_im = ImageCreateTrueColor($dst_w, $dst_h);
ImageCopyResampled($dst_im, $src_im, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
ImageJpeg($dst_im);
ImageDestroy($dst_im);
imageDestroy($src_im);

答案 2 :(得分:1)

有一个名为PHP Image Magician的简单易用的开源库,它有一些不错的功能和文档。

它使用100%GD。

基础用法示例:

$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');

答案 3 :(得分:0)

如果您需要更多功能,convert可能会有帮助。