首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过php脚本调整图片大小

如何通过php脚本调整图片大小
EN

Stack Overflow用户
提问于 2013-07-23 13:29:18
回答 7查看 15.1K关注 0票数 6

我正在创建一个应用程序,这需要通过php脚本转换为缩略图的大图像,然后将其编码为base64,这样我就可以通过json发送到我的安卓应用程序。我在调整图像大小时遇到了问题。我需要php脚本来帮助我做到这一点

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2013-07-23 14:03:16

您可以尝试此图像调整函数tutorial

你也可以使用这个代码来调整大小函数(GD)。

代码语言:javascript
运行
复制
<?php

$thumb = new Imagick();
$thumb->readImage('myimage.gif');    $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy(); 

?>

Or, a shorter version of the same:

<?php

$thumb = new Imagick('myimage.gif');

$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');

$thumb->destroy(); 

?>

也请参考此链接以调整图像大小

1.YETANOTHERLinks

2.9Lession

也可以参考这个Links来转换图像的Base64

票数 7
EN

Stack Overflow用户

发布于 2013-07-23 14:35:29

下面的代码创建一个名为createThumbs的函数,该函数将获得三个参数。第一个和第二个对应的是包含原始图像的目录的路径和放置缩略图的目录的路径。第三个参数是缩略图的宽度。

代码语言:javascript
运行
复制
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' ) 
    {
      echo "Creating thumbnail for {$fname} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
}
// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>
票数 5
EN

Stack Overflow用户

发布于 2013-07-23 13:36:19

您可以尝试imagick - http://php.net/manual/en/imagick.resizeimage.php来调整图像大小。示例代码:创建缩略图:

代码语言:javascript
运行
复制
<?php

$thumb = new Imagick();
$thumb->readImage('myimage.gif');    
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy(); 

?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17802106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档