search
Homephp教程php手册php简单缩略图类|image.class.php

 使用方法:


$img = new iamge;
$img->resize('dstimg.jpg', 'srcimg.jpg', 300, 400);
说明:这个是按照比例缩放,dstimg.jpg是目标文件,srcimg.jpg是源文件,后面的是目标文件的宽和高
$img->thumb('dstimg.jpg', 'scrimg.jpg', 300, 300);
说明:这个是按照比例缩略图,比如常用在头像缩略图的时候,dstimg.jpg是目标文件,srcimg.jpg是源文件,后面的是目标文件的宽和高
这个是针对GD库才这样麻烦的,如果采用Imagick的话,就只需要两个函数就实现上面的功能,去查下文档就找到了。

 

class image{
 
 public function resize($dstImg, $srcImg, $dstW, $dstH){
  list($srcW, $srcH) = getimagesize($srcImg);
  $scale = min($dstW/$srcW, $dstH/$srcH);
        $newW = round($srcW * $scale);
        $newH = round($srcH * $scale);
  $newImg = imagecreatetruecolor($newW, $newH);
  $srcImg = imagecreatefromjpeg($srcImg);
  imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newW, $newH, $srcW, $srcH);
  imagejpeg($newImg, $dstImg);
 }
 
 public function thumb($dstImg, $srcImg, $dstW, $dstH){
  list($srcW, $srcH) = getimagesize($srcImg);
  $scale = max($dstW/$srcW, $dstH/$srcH);
  $newW = round($dstW/$scale);
  $newH = round($dstH/$scale);
  $x = ($srcW - $newW)/2;
  $y = ($srcH - $newH)/2;
  $newImg = imagecreatetruecolor($dstW, $dstH);
  $srcImg = imagecreatefromjpeg($srcImg);
  imagecopyresampled($newImg, $srcImg, 0, 0, $x, $y, $dstW, $dstH, $newW, $newH);
  imagejpeg($newImg, $dstImg);
 }
  
}

function createFromType($type, $srcImg){
 $function = "imagecreatefrom$type";
 return $function($srcImg);
}
//为了解决不同图片格式的问题

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),