插件13:生成缩略图

<?php // Plug-in 13: Make Thumbnail

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
// You will need a jpeg image file called test.jpg in this folder

$image = imagecreatefromjpeg("test.jpg");
$thumb = PIPHP_MakeThumbnail($image, 100);
imagejpeg($thumb, "thumb.jpg");

$thumb2 = PIPHP_MakeThumbnail($image, 50);
imagejpeg($thumb2, "thumb2.jpg");

echo <<<_END
Here are the original and thumbnail images:<br /><img
src='test.jpg' align='left'>  <img src='thumb.jpg'>
<br /><br />This thumbnail has maximum<br />dimensions of 100
pixels.<br /><br />Here's another with a maximum<br />50 pixels
width and height:<br /><br /><img src='thumb2.jpg'>
_END;

function PIPHP_MakeThumbnail($image, $max)
{
   // Plug-in 13: Make Thumbnail
   //
   // This plug-in takes a GD image and returns a copy as
   // a thumbnail. The arguments required are:
   //
   //    $image: The image source
   //    $max:   Maximum width and height
   
   $thumbw = $w = imagesx($image);
   $thumbh = $h = imagesy($image);

   if ($w > $h && $max < $w)
   {
      $thumbh = $max / $w * $h;
      $thumbw = $max;
   }
   elseif ($h > $w && $max < $h)
   {
      $thumbw = $max / $h * $w;
      $thumbh = $max;
   }
   elseif ($max < $w)
   {
      $thumbw = $thumbh = $max;
   }

   return PIPHP_ImageResize($image, $thumbw, $thumbh);
}

// The function below is repeated here
// just for the sake of this example as it
// is called by it.

function PIPHP_ImageResize($image, $w, $h)
{
   // Plug-in 12: Image Resize
   // This plug-in takes a GD image and resizes it to the
   // required dimensions. The arguments are:
   //    $image: The image source
   //    $w:     New width 
   //    $h:     New height
   
   $oldw = imagesx($image);
   $oldh = imagesy($image);
   $temp = imagecreatetruecolor($w, $h);
   imagecopyresampled($temp, $image, 0, 0, 0, 0,
      $w, $h, $oldw, $oldh);
   return $temp;
}

?>

插件说明:

插件13接受两个参数。一个是需要转换为缩略图的图像,另一个是缩略图的最大宽度或高度,据体如下:

$image 一个需要转换的GD图像。

$max 缩略图的最大宽度或高度(取决于那个比较大)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值