百科狗-知识改变命运!
--

imagecopyresized() - gd函数(图像处理)

梵高12个月前 (11-21)阅读数 12#技术干货
文章标签图像

imagecopyresized()

(PHP 4, PHP 5, PHP 7)

拷贝部分图像并调整大小

说明

imagecopyresized(resource $dst_image,resource $src_image,int $dst_x,int $dst_y,int $src_x,int $src_y,int $dst_w,int $dst_h,int $src_w,int $src_h): bool

imagecopyresized()将一幅图像中的一块矩形区域拷贝到另一个图像中。$dst_image和$src_image分别是目标图像和源图像的标识符。

In other words,imagecopyresized() will take an rectangular area from$src_imageof width$src_wand height$src_hat position($src_x,$src_y)and place it in a rectangular area of$dst_imageof width$dst_wand height$dst_hat position($dst_x,$dst_y).

如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果$dst_image和$src_image相同的话)区域,但如果区域交迭的话则结果不可预知。

参数

$dst_image

imagecopyresized() - gd函数(图像处理)

目标图象连接资源。

$src_image

源图象连接资源。

$dst_x

x-coordinate of destination point.

$dst_y

y-coordinate of destination point.

$src_x

x-coordinate of source point.

$src_y

y-coordinate of source point.

$dst_w

Destination width.

$dst_h

Destination height.

$src_w

源图象的宽度。

$src_h

源图象的高度。

返回值

成功时返回TRUE,或者在失败时返回FALSE

范例

Resizing an image

这个例子会以一半的尺寸显示图片

以上例程的输出类似于:

The image will be output at half size, though better quality could be obtained using imagecopyresampled().

注释

Note:

因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用imagecreatetruecolor()创建的。

参见

imagecopyresampled() 重采样拷贝部分图像并调整大小

Most of the examples below don't keep the proportions properly. They keep using if/else for the height/width..and forgetting that you might have a max height AND a max width, not one or the other.
/**
* Resize an image and keep the proportions
* @author Allison Beckwith 
* @param string $filename
* @param integer $max_width
* @param integer $max_height
* @return image
*/
function resizeImage($filename, $max_width, $max_height)
{
  list($orig_width, $orig_height) = getimagesize($filename);
  $width = $orig_width;
  $height = $orig_height;
  # taller
  if ($height > $max_height) {
    $width = ($max_height / $height) * $width;
    $height = $max_height;
  }
  # wider
  if ($width > $max_width) {
    $height = ($max_width / $width) * $height;
    $width = $max_width;
  }
  $image_p = imagecreatetruecolor($width, $height);
  $image = imagecreatefromjpeg($filename);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, 
                   $width, $height, $orig_width, $orig_height);
  return $image_p;
}
Belows is the code snipet that allows you to resize a transparent PNG and composite it into another image. The code is tested to work with PHP5.1.2, GD2, but I think it can also work with other versions of PHP and GD. 
The code has been commented to help you read through it. The idea of resizing the transparent PNG image is to create a new destination image which is completely transparent then turn off the imageAlphaBlending of this new image so that when the PNG source file is copied, its alpha channel is still retained.

Example usage: 
This Class image resize or crop,
your copyright text write on the image and your logo marge on the image.
Full Class code => http://www.phpbuilder.com/snippet/download.php?type=snippet&id=2188
/**
   * Author     : Halit YESIL
   * www      : www.halityesil.com, www.prografik.net, www.trteknik.net
   * email    : mail@halityesil.com, halit.yesil@prografik.net, halit.yesil@trteknik.net
   * GSM      : +90.535 648 3504
   * Create Date  : 21 / 03 / 2007
   * 
   * 
   * Script    : Image resize and croping class. 
   *         You can Write copyright text and attach your logo write on image with this class
   * 
   * 
   * $obj = new pg_image($arg);
   *
   * 
   * Varibles Information
   *     [0] type => (POST | FILE) => Source File Send Type _FILES OR Dir
   *     [1] file => ({if POST Array file = $_FILES['yourfile']}|{if FILE false}) => Source File filesource
   *     [2] path => ({if FILE String [ dirname/filename.extension ]}) => Source File Root Path
   *     [3] target => ({if FILE String [ dirname/filename.extension ]}) => Target File Root Path
   *     [4] width => (integer) => Target File Width
   *     [5] height => (integer) => Target File Height
   *     [6] quality => (integer 1 - 100) => Target File Quality 0-100 %
   *     [7] action => (crop | resize) => Target Action "resize" OR "crop"
   *     [8] bordersize => (integer 0-5) => Target Border Size
   *     [9] bordercolor => (color hex) => Target Border Color
   *     [10] bgcolor => (color hex) => Target Background Color
   *     [11] copytext => (String) => Copyright Content Text
   *     [12] copycolor => (color hex) => Copyright Text Color
   *     [13] copyshadow => (color hex) => Copyright Text Shadow color
   *     [14] copybgcolor => (color hex) => Copyright Background Color
   *     [15] copybgshadow => (color hex) => Copyright Background Shadow Color
   *     [16] copybordersize => (integer 0-5) => Copyright Border Size 1-3
   *     [17] copybordercolor => (color hex) => Copyright Border Color
   *   [18] copyposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft) => Copyright Position
   *     [19] logoposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft) => Logo Image Position
   *     [20] logoimage => (String [ dirname/filename.extension] Allow Extension (PNG | GIF | JPG)) => Logo Image Root Path "PNG" OR "GIF" OR "JPG"
   * 
   * 
   * 
   * 
   */ 
This snippet allows you to grab a thumbnail from the center of a large image. This was used for a client photo gallery for art to give a teaser of the image to come (only a small portion). You could get dynamic with this value. I also put in a scaling factor in case you want to scale down first before chopping. 
Resize image proportionaly where you give a max width or max height 
If you read your Imagedata from a Database Blob and use the functions from above to resize the image to a thumbnail improving a lot of traffic, you will have to make temporary copies of the files in order that the functions can access them
Here a example:
// switch through imagetypes
$extension = "jpg";
if(mysql_result($query, 0, 'type') == "image/pjpeg")
{ $extension = "jpg"; } // overwrite
else if(mysql_result($query, 0, 'type') == "image/gif")
{ $extension = "gif"; } // overwrite
// get a temporary filename
// use microtime() to get a unique filename
// if you request more than one file f.e. by creating large numbers of thumbnails, the server could be not fast enough to save all these different files and you get duplicated copies and resizepics() will resize and output often the same content
$filename = microtime()."_temp.".$extension;
// open
$tempfile = fopen($filename, "w+");
// write
fwrite($tempfile, mysql_result($query, 0, 'image'));
// close
fclose($tempfile);
// resize and output the content
echo resizepics($filename, '100', '70');
// delete temporary file
unlink($filename);
NOTE: this script has to be put into a file which sends correct header informations to the browser, otherwise you won't get more to see than a big red cross :-)
If you need to delete or resize images in the filesystem (not in DB) without loosing the image quality...
I commented the code as much as possible so that newbies (like myself) will understand it. ;)

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)