php generate image thumbnail function

高洛峰
Release: 2023-03-06 06:22:02
Original
1645 people have browsed it

The example in this article describes the function of generating image thumbnails in PHP. Share it with everyone for your reference, the details are as follows:

 <?php
/*
 * Created on 2011-3-18
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
*/
/*构造函数-生成缩略图+水印,参数说明:
$srcFile-图片文件名,
$dstFile-另存文件名,
$markwords-水印文字,
$markimage-水印图片,
$dstW-图片保存宽度,
$dstH-图片保存高度,
$rate-图片保存品质*/
makethumb("1.jpg", "aa/b.jpg", "50", "50");
function makethumb($srcFile, $dstFile, $dstW, $dstH, $rate = 100, $markwords = null, $markimage = null) {
  $data = GetImageSize($srcFile);
  switch ($data[2]) {
    case 1:
      $im = @ImageCreateFromGIF($srcFile);
    break;
    case 2:
      $im = @ImageCreateFromJPEG($srcFile);
    break;
    case 3:
      $im = @ImageCreateFromPNG($srcFile);
    break;
  }
  if (!$im) return False;
  $srcW = ImageSX($im);
  $srcH = ImageSY($im);
  $dstX = 0;
  $dstY = 0;
  if ($srcW * $dstH > $srcH * $dstW) {
    $fdstH = round($srcH * $dstW / $srcW);
    $dstY = floor(($dstH - $fdstH) / 2);
    $fdstW = $dstW;
  } else {
    $fdstW = round($srcW * $dstH / $srcH);
    $dstX = floor(($dstW - $fdstW) / 2);
    $fdstH = $dstH;
  }
  $ni = ImageCreateTrueColor($dstW, $dstH);
  $dstX = ($dstX < 0) ? 0 : $dstX;
  $dstY = ($dstX < 0) ? 0 : $dstY;
  $dstX = ($dstX > ($dstW / 2)) ? floor($dstW / 2) : $dstX;
  $dstY = ($dstY > ($dstH / 2)) ? floor($dstH / s) : $dstY;
  $white = ImageColorAllocate($ni, 255, 255, 255);
  $black = ImageColorAllocate($ni, 0, 0, 0);
  imagefilledrectangle($ni, 0, 0, $dstW, $dstH, $white); // 填充背景色
  ImageCopyResized($ni, $im, $dstX, $dstY, 0, 0, $fdstW, $fdstH, $srcW, $srcH);
  if ($markwords != null) {
    $markwords = iconv("gb2312", "UTF-8", $markwords);
    //转换文字编码
    ImageTTFText($ni, 20, 30, 450, 560, $black, "simhei.ttf", $markwords); //写入文字水印
    //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
     
  } elseif ($markimage != null) {
    $wimage_data = GetImageSize($markimage);
    switch ($wimage_data[2]) {
      case 1:
        $wimage = @ImageCreateFromGIF($markimage);
      break;
      case 2:
        $wimage = @ImageCreateFromJPEG($markimage);
      break;
      case 3:
        $wimage = @ImageCreateFromPNG($markimage);
      break;
    }
    imagecopy($ni, $wimage, 500, 560, 0, 0, 88, 31); //写入图片水印,水印图片大小默认为88*31
    imagedestroy($wimage);
  }
  ImageJpeg($ni, $dstFile, $rate);
  ImageJpeg($ni, $srcFile, $rate);
  imagedestroy($im);
  imagedestroy($ni);
}
?>
Copy after login

I hope this article will be helpful to everyone in PHP programming design.

For more articles related to PHP’s function of generating image thumbnails, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!