用php调整图像大小?

发布于 2024-12-06 06:06:49 字数 838 浏览 7 评论 0原文

我在网上和这里进行了研究,但我一直没有找到可以帮助我的答案: 我有下面的代码,它根据用户的位置显示文件夹中的图像,但是图像太大,我需要调整它的大小。 我尝试或阅读的所有脚本都与上传的文件有关。谁能把我推向正确的方向?

谢谢。

 <?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>

ive done my research on the net and here as well and i keep on coming up with no answer that can help me:
i have the below code which displays an image from a folder based on a user's location however the image is too big and i need to resize it.
all the scripts that i have tried or read relate to files being uploaded. can anyone push me in the right direction?

thank you.

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

My problem arises from the fact that i need to pull the images as:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不离久伴 2024-12-13 06:06:49

不久前写了一个关于此的教程。也许它能有所帮助。它从上传开始,但大部分是关于调整大小。只需通过以不同的方式获取图像类型和文件名来交换 $_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 /

Wrote a tutorial about this a while ago. Perhaps it can help. It starts with uploading, but most of it is about resizing. Just swap out the usage of the $_FILES array by geting the image type and file name a different way. Here's the code you should need:

// 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;

If you want to store the image as a file rather than dumping it to the browser, remove the head and echo part at the end and then swap out the NULL parameter in the imagejpeg call with an actual filename. Hope that helps :)

Here's the code in use: http://samples.geekality.net/image-resize/

我三岁 2024-12-13 06:06:49

你看一下 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);

You make take a look at gd : http://www.php.net/manual/en/function.imagecopyresized.php

You can try this:

$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);
成熟稳重的好男人 2024-12-13 06:06:49

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

它使用 100% GD。

基础用法示例:

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

There a simple to use, open source library called PHP Image Magician that has some nice features and documentation.

It uses 100% GD.

Example of basis usage:

$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');
对你的占有欲 2024-12-13 06:06:49

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

If you need more features convert might be a help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文