Home  >  Article  >  Backend Development  >  PHP generates thumbnails and adds watermark_PHP tutorial

PHP generates thumbnails and adds watermark_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:07:20953browse

This is a simple GD library operation

[php]
/****
Yan Shiba Public Welfare PHP Lecture
Forum: http://www.zixue.it
Weibo: http://weibo.com/Yshiba
YY Channel: 88354001
****/
/***
Want to manipulate pictures
You must first get the size and type information of the image
Watermark: Copy the specified watermark to the target and add a transparent effect
Thumbnail: It is to copy a large picture to a small size screen
***/
class ImageTool {
// imageInfo analyzes image information
// return array()
public static function imageInfo($image) {
// Determine whether the image exists
if (!file_exists($image)) {
return false;
} }
$info = getimagesize($image);
if ($info == false) {
return false;
} }
// At this time, the info is analyzed and it is an array
$img['width'] = $info[0];
$img['height'] = $info[1];
$img['ext'] = substr($info['mime'], strpos($info['mime'], '/') + 1);
return $img;
}
/*
Watermark function
parm String $dst and other operation pictures
parm String $water watermark image
parm String $save, if not filled in, the original image will be replaced by default
*/
public static function water($dst, $water, $save = NULL, $pos = 2, $alpha = 50) {
// Make sure 2 pictures exist first
if (!file_exists($dst) || !file_exists($water)) {
return false;
} }
// First ensure that the watermark cannot be larger than the image to be operated
$dinfo = self::imageInfo($dst);
$winfo = self::imageInfo($water);
if ($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) {
return false;
} }
// Two pictures, read them on the canvas! But the pictures may be png or jpeg, what function should be used to read them?
$dfunc = 'imagecreatefrom' . $dinfo['ext'];
$wfunc = 'imagecreatefrom' . $winfo['ext'];
if (!function_exists($dfunc) || ​​!function_exists($wfunc)) {
return false;
} } www.2cto.com
// Dynamically load functions to create canvas
$dim = $dfunc($dst);
// Create the canvas to be operated
$wim = $wfunc($water);
// Create watermark canvas
// Calculate the pasted coordinates based on the location of the watermark
switch($pos) {
case 0 :
                                                                               
$posx = 0;
                $posy = 0;  
                break;  
  
            case 1 :  
                // 右上角  
                $posx = $dinfo['width'] - $winfo['width'];  
                $posy = 0;  
                break;  
  
            case 3 :  
                // 左下角  
                $posx = 0;  
                $posy = $dinfo['height'] - $winfo['height'];  
                break;  
  
            default :  
                $posx = $dinfo['width'] - $winfo['width'];  
                $posy = $dinfo['height'] - $winfo['height'];  
        }  
  
        // 加水印  
        imagecopymerge($dim, $wim, $posx, $posy, 0, 0, $winfo['width'], $winfo['height'], $alpha);  
  
        // 保存  
        if (!$save) {  
            $save = $dst;  
            unlink($dst);  
            // 删除原图  
        }  
  
        $createfunc = 'image' . $dinfo['ext'];  
        $createfunc($dim, $save);  
  
        imagedestroy($dim);  
        imagedestroy($wim);  
  
        return true;  
    }  
  
    /**
Thumb Generate thumbnails
Scale proportionally, leave blank on both sides
**/  
    public static function thumb($dst, $save = NULL, $width = 200, $height = 200) {  
        // 首先判断待处理的图片存不存在  
        $dinfo = self::imageInfo($dst);  
        if ($dinfo == false) {  
            return false;  
        }  
  
        // 计算缩放比例  
        $calc = min($width / $dinfo['width'], $height / $dinfo['height']);  
  
        // 创建原始图的画布  
        $dfunc = 'imagecreatefrom' . $dinfo['ext'];  
        $dim = $dfunc($dst);  
  
        // 创建缩略画布  
        $tim = imagecreatetruecolor($width, $height);  
  
        // 创建白色填充缩略画布  
        $white = imagecolorallocate($tim, 255, 255, 255);  
  
        // 填充缩略画布  
        imagefill($tim, 0, 0, $white);  
  
        // 复制并缩略  
        $dwidth = (int)$dinfo['width'] * $calc;  
        $dheight = (int)$dinfo['height'] * $calc;  
  
        $paddingx = (int)($width - $dwidth) / 2;  
        $paddingy = (int)($height - $dheight) / 2;  
  
        imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $dinfo['width'], $dinfo['height']);  
  
        // 保存图片  
        if (!$save) {  
            $save = $dst;  
            unlink($dst);  
        }  
  
        $createfunc = 'image' . $dinfo['ext'];  
        $createfunc($tim, $save);  
  
        imagedestroy($dim);  
        imagedestroy($tim);  
  
        return true;  
  
    }  
  
}  
  
// print_r(ImageTool::imageInfo('./home.jpg'));  
/*  www.2cto.com
 echo ImageTool::water('./home.jpg','./smallfeng.png','home1.jpg',0)?'OK':'FAIL'; 
 echo ImageTool::water('./home.jpg','./smallfeng.png','home2.jpg',1)?'OK':'FAIL'; 
 echo ImageTool::water('./home.jpg','./smallfeng.png','home3.jpg',2)?'OK':'FAIL'; 
 echo ImageTool::water('./home.jpg','./smallfeng.png','home4.jpg',3)?'OK':'FAIL'; 
 */  
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477858.htmlTechArticle这个一个简单的GD库操作 [php] ?php /**** Yanshiba Public Welfare PHP Lecture Forum: http://www.zixue.it Weibo: http://weibo.com/Yshiba YY Channel: 88354001 ****/ /*** 想操作图片...
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