Create thumbnails and copyright classes for pictures

Source: Internet
Author: User
Tags add watermark to picture array functions imagejpeg strlen
Thumbnail image

Create thumbnails and copyright classes for pictures

Recently a few days to look at the image processing of PHP features, the previous demand in this area is relatively small, there is no way to see, recently have a look. Feel the picture processing some simple function also can, complex even, GD storehouse all 2.0.1, still do not support Chinese, read a few articles, want to use Chinese only first convert GB2312 to Unicode again write picture, too troublesome, simply use English to forget.

In the Image generation section, you can define the maximum height and width of the picture, which is suitable for news and photo albums and other systems.

GD2.0.1 in the image processing has been greatly improved, I tried the next imagecopyresized and Imagecopyresampled, the latter deal with the picture is significantly better than the former, according to the manual that the latter to change the size of the picture after the basic remains not distorted, the effect of generating thumbnails is really good.

-------------------------------------------------------------
Below is the class
-----------------------
//====================================================
FileName:GDImage.inc.php
Summary: Picture processing program
Author:ice_berg16 (Scarecrow seeking the Dream)
Createtime:2004-10-12
Lastmodifed:2004-10-12
Copyright (c) ice_berg16@163.com
//====================================================

Class Gdimage
{
var $sourcePath; Picture Storage Path
var $galleryPath; Picture Thumbnails storage Path
var $toFile = false; Whether to generate files
var $fontName; TTF font name to use
var $maxWidth = 500; Picture Maximum width
var $maxHeight = 600; Picture Max height

//==========================================
Functions: Gdimage ($sourcePath, $galleryPath, $fontPath)
Function: Constructor
Parameters: $sourcePath Picture source path (including last "/")
Parameters: $galleryPath The path to build a picture
Parameters: $fontPath Font path
//==========================================
function Gdimage ($sourcePath, $galleryPath, $fontPath)
{
$this->sourcepath = $sourcePath;
$this->gallerypath = $galleryPath;
$this->fontname = $fontPath. "04b_08__. TTF ";
}

//==========================================
Functions: Makethumb ($sourFile, $width =128, $height =128)
function: Generate thumbnails (output to browser)
Parameters: $sourFile Picture source file
Parameters: $width to generate the width of the thumbnail
Parameters: The height of the thumbnail $height generated
Return: 0 Returns the generated picture path when failure succeeds
//==========================================
function Makethumb ($sourFile, $width =128, $height =128)
{
$imageInfo = $this->getinfo ($sourFile);
$sourFile = $this->sourcepath. $sourFile;
$newName = substr ($imageInfo ["name"], 0, Strrpos ($imageInfo ["name"], "."). "_thumb.jpg";
Switch ($imageInfo ["type"])
{
Case 1://gif
$img = Imagecreatefromgif ($sourFile);
Break
Case 2://jpg
$img = Imagecreatefromjpeg ($sourFile);
Break
Case 3://png
$img = Imagecreatefrompng ($sourFile);
Break
Default
return 0;
Break
}
if (! $img)
return 0;

$width = ($width > $imageInfo ["width"])? $imageInfo ["width"]: $width;
$height = ($height > $imageInfo ["height"])? $imageInfo ["Height"]: $height;
$srcW = $imageInfo ["width"];
$srcH = $imageInfo ["Height"];
if ($srcW * $width > $srcH * $height)
$height = Round ($srcH * $width/$srcW);
Else
$width = Round ($srcW * $height/$srcH);
//*
if (function_exists ("Imagecreatetruecolor"))//gd2.0.1
{
$new = Imagecreatetruecolor ($width, $height);
Imagecopyresampled ($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"]);
}
Else
{
$new = Imagecreate ($width, $height);
Imagecopyresized ($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"]);
}
//*/
if ($this->tofile)
{
if (file_exists ($this->gallerypath. $newName))
Unlink ($this->gallerypath. $newName);
Imagejpeg ($new, $this->gallerypath. $newName);
Return $this->gallerypath. $newName;
}
Else
{
Imagejpeg ($new);
}
Imagedestroy ($new);
Imagedestroy ($IMG);

}
//==========================================
Functions: Watermark ($sourFile, $text)
Function: Add watermark to Picture
Parameters: $sourFile picture file name
Parameters: An array of $text literals (two strings included)
Return: 1 Returns the generated picture path when successful
//==========================================
function watermark ($sourFile, $text)
{
$imageInfo = $this->getinfo ($sourFile);
$sourFile = $this->sourcepath. $sourFile;
$newName = substr ($imageInfo ["name"], 0, Strrpos ($imageInfo ["name"], "."). "_mark.jpg";
Switch ($imageInfo ["type"])
{
Case 1://gif
$img = Imagecreatefromgif ($sourFile);
Break
Case 2://jpg
$img = Imagecreatefromjpeg ($sourFile);
Break
Case 3://png
$img = Imagecreatefrompng ($sourFile);
Break
Default
return 0;
Break
}
if (! $img)
return 0;

$width = ($this->maxwidth > $imageInfo ["width"])? $imageInfo ["width"]: $this->maxwidth;
$height = ($this->maxheight > $imageInfo ["height"])? $imageInfo ["Height"]: $this->maxheight;
$srcW = $imageInfo ["width"];
$srcH = $imageInfo ["Height"];
if ($srcW * $width > $srcH * $height)
$height = Round ($srcH * $width/$srcW);
Else
$width = Round ($srcW * $height/$srcH);
//*
if (function_exists ("Imagecreatetruecolor"))//gd2.0.1
{
$new = Imagecreatetruecolor ($width, $height);
Imagecopyresampled ($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"]);
}
Else
{
$new = Imagecreate ($width, $height);
Imagecopyresized ($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"]);
}
$white = Imagecolorallocate ($new, 255, 255, 255);
$black = imagecolorallocate ($new, 0, 0, 0);
$alpha = Imagecolorallocatealpha ($new, 230, 230, 230, 40);
$rectW = Max (strlen ($text [0]), strlen ($text [1])) *7;
Imagefilledrectangle ($new, 0, $height -26, $width, $height, $alpha);
Imagefilledrectangle ($new, $height -20, $height-7, $black);
Imagettftext ($new, 4.9, 0, $height -14, $black, $this->fontname, $text [0]);
Imagettftext ($new, 4.9, 0, $height-6, $black, $this->fontname, $text [1]);
//*/
if ($this->tofile)
{
if (file_exists ($this->gallerypath. $newName))
Unlink ($this->gallerypath. $newName);
Imagejpeg ($new, $this->gallerypath. $newName);
Return $this->gallerypath. $newName;
}
Else
{
Imagejpeg ($new);
}
Imagedestroy ($new);
Imagedestroy ($IMG);

}
//==========================================
Functions: Displaythumb ($file)
Function: Display thumbnail of the specified picture
Parameters: $file file name
Back: 0 picture does not exist
//==========================================
function Displaythumb ($file)
{
$thumbName = substr ($file, 0, Strrpos ($file, ".")). "_thumb.jpg";
$file = $this->gallerypath. $thumbName;
if (!file_exists ($file))
return 0;
$html = "";
Echo $html;
}
//==========================================
Functions: Displaymark ($file)
Function: Display the watermark of the specified picture
Parameters: $file file name
Back: 0 picture does not exist
//==========================================
function Displaymark ($file)
{
$markName = substr ($file, 0, Strrpos ($file, ".")). "_mark.jpg";
$file = $this->gallerypath. $markName;
if (!file_exists ($file))
return 0;
$html = "";
Echo $html;
}
//==========================================
Functions: GetInfo ($file)
Function: Return image information
Parameters: $file File path
Back: array of picture information
//==========================================
function GetInfo ($file)
{
$file = $this->sourcepath. $file;
$data = getimagesize ($file);
$imageInfo ["width"] = $data [0];
$imageInfo ["Height"]= $data [1];
$imageInfo ["type"] = $data [2];
$imageInfo ["name"] = basename ($file);
return $imageInfo;
}

}

?>
----------------------------------
Here's how to use
This class uses a 04b_08__. TTF fonts
Specify the font path when using the class
-----------------------------------
Require_once ("GDImage.inc.php");

Header ("Content-type:image/jpeg");//output to browser don't forget to open this.
$img = new Gdimage (Ib_upload_path, Ib_gallery, ib_temp);
$text = Array ("ice-berg.org", "All Rights reserved");
$img->maxwidth = $img->maxheight = 300;
$img->tofile = true;
$img->watermark ("Mm.jpg", $text);
$img->makethumb ("mm.jpg");
$img->displaythumb ("mm.jpg");
$img->displaymark ("mm.jpg");



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.