PHP thumbnail generate several methods code _php tutorial

Source: Internet
Author: User
Tags fread image identifier imagejpeg unpack
1. From which we can see the role of the Imagecreatetruecolor function is obviously to create a black background image, its first parameter is the width of the created picture, the second parameter is the height of the created picture, we put the function's return value (image identifier) into the variable inside.

2.imagecreatefromjpeg is the image that will be split to read into memory (here you may have a question: I directly from the hard micro disk to read it, why should I read in memory?) to make an inappropriate analogy, Everyone usually in the money when I believe we will not put too much in the pocket, generally to use the time to take from the bank, here is the same, this picture does not use it when I put it in the hard disk, when the picture to be divided or other operations when it read into the memory inside, plainly, Memory provides a running stage for the program)

3. Look at the imagecopyresampled function it is to split the original image, and then it and the sample copy (I understand as a projection) to create a good background image with Imagecreatefromjpeg.

The file
$filename = ' temp/sunset.jpg ';
$percent = 0.5;
Content type
Header (' Content-type:image/jpeg ');
Get New Dimensions
List ($width, $height) = getimagesize ($filename);
$new _width = $width * $percent;
$new _height = $height * $percent;

To create a new picture with the width of the original picture of 0.5 for the new length of the logo for this picture is $image_p
$image _p = Imagecreatetruecolor ($new _width, $new _height);
Create a new image from a JPEG file or URL
$image = Imagecreatefromjpeg ($filename);
Split the original picture from the coordinates (100,100), the length of the split (400), the height of (300) half of the original picture, and place the segmented image in the established area starting from the coordinates (0,0).
Imagecopyresampled ($image _p, $image, 0, 0, $new, _width, $new _height, 400, 300);

Output
Imagejpeg ($image _p, NULL,//quality), the quality range for the picture output is from 0 (worst quality, file smaller) to 100 (best quality, File max).
?>

The above example is to divide the $image image from the coordinates (100,100), the width of the partition is 400, the height is 300, and then the picture from the coordinates (0,0) from the projection to the picture $image_p, the width of the projection is $new_width, the height is $new_ Height

File and Zoom dimensions
$imgfile = ' smp.jpg ';
$percent = 0.2;
Header (' Content-type:image/jpeg ');
List ($width, $height) = getimagesize ($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = Imagecreatetruecolor ($newwidth, $newheight);
$source = Imagecreatefromjpeg ($imgfile);
Imagecopyresized ($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
Imagejpeg ($THUMB);
?>

More detailed tutorials

/* constructor-Generate thumbnail + watermark, parameter description:
$srcFile-picture file name,
$dstFile-Save file name,
$markwords-Watermark text,
$markimage-Watermark picture,
$dstW-Picture Save width,
$dstH-Picture Save height,
$rate-Picture Preservation quality */
Makethumb ("A.jpg", "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);//Fill background color
Imagecopyresized ($ni, $im, $dstX, $dstY, 0,0, $fdstW, $fdstH, $srcW, $srcH);
if ($markwords!=null)
{
$markwords =iconv ("gb2312", "UTF-8", $markwords);
Convert text Encoding
Imagettftext ($ni, 20,30,450,560, $black, "Simhei.ttf", $markwords); Write a text watermark
parameter is, text size | deflection | horizontal | ordinate | text color | text Type | text content
}
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); Write picture watermark, watermark image size "> picture size default to 88*31
Imagedestroy ($wimage);
}
Imagejpeg ($ni, $dstFile, $rate);
Imagejpeg ($ni, $srcFile, $rate);
Imagedestroy ($im);
Imagedestroy ($ni);
}
?>

Example Four

$thumbnail = new Imageresize ();
$thumbnail->resizeimage (source picture full path, thumbnail width, thumbnail height, whether trimmed (0 or 1), new picture full path);

Class Imageresize {

Type of picture
var $type;

Actual width
var $width;

Actual height
var $height;

The width after the change
var $resize _width;

The height after the change
var $resize _height;

Whether or not to cut the map
var $cut;

Source image
var $srcimg;

Destination image Address
var $dstimg;

Temporarily created images
var $im;

function Resizeimage ($img, $wid, $hei, $c, $dstpath) {
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;

Type of picture
$this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1);

Initializing an image
$this->initi_img ();

Destination image Address
$this-Dst_img ($dstpath);

//--
$this->width = imagesx ($this->im);
$this->height = Imagesy ($this->im);

Creating images
$this->newimg ();

Imagedestroy ($this->im);
}

function newimg () {

The proportions of the altered image
$resize _ratio = ($this->resize_width)/($this->resize_height);

Ratio of actual images
$ratio = ($this->width)/($this->height);

if ($this->cut) = = "1") {
Drawing Height Priority
if ($ratio >= $resize _ratio) {
$newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this Height) * $resize _ratio), $this->height);
Imagejpeg ($newimg, $this->dstimg);
}

Drawing width First
if ($ratio < $resize _ratio) {
$newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width , (($this->width)/$resize _ratio));
Imagejpeg ($newimg, $this->dstimg);
}
} else {
Do not cut the map
if ($ratio >= $resize _ratio) {
$newimg = Imagecreatetruecolor ($this->resize_width, ($this->resize_width)/$ratio);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this- >width, $this->height);
Imagejpeg ($newimg, $this->dstimg);
}
if ($ratio < $resize _ratio) {
$newimg = Imagecreatetruecolor (($this->resize_height) * $ratio, $this->resize_height);
Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $ This->width, $this->height);
Imagejpeg ($newimg, $this->dstimg);
}
}
}

Initializing an image
function initi_img () {
if ($this->type== "jpg") {
$this->im = imagecreatefromjpeg ($this->srcimg);
}

if ($this->type== "gif") {
$this->im = imagecreatefromgif ($this->srcimg);
}

if ($this->type== "png") {
$this->im = imagecreatefrompng ($this->srcimg);
}

if ($this->type== "bmp") {
$this->im = $this->imagecreatefrombmp ($this->srcimg);
}
}

Image Destination Address
function Dst_img ($dstpath) {
$full _length = strlen ($this->srcimg);
$type _length = strlen ($this->type);
$name _length = $full _length-$type _length;
$name = substr ($this->srcimg,0, $name _length-1);
$this->dstimg = $dstpath;
Echo $this->dstimg;
}

function Convertbmp2gd ($src, $dest = False) {
if (! ( $src _f = fopen ($SRC, "RB")) {
return false;
}
if (! ( $dest _f = fopen ($dest, "WB")) {
return false;
}
$header = Unpack ("Vtype/vsize/v2reserved/voffset", Fread ($src _f,14));
$info = Unpack ("Vsize/vwidth/vheight/vplanes/vbits/vcompression/vimagesize/vxres/vyres/vncolor/vimportant", fread ($src _f, 40));

Extract ($info);
Extract ($header);

if ($type! = 0x4d42) {//signature "BM"
return false;
}

$palette _size = $offset-54;
$ncolor = $palette _SIZE/4;
$GD _header = "";
True-color vs. Palette
$GD _header. = ($palette _size = = 0)? "Xffxfe": "Xffxff";
$GD _header. = Pack ("N2", $width, $height);
$GD _header. = ($palette _size = = 0)? "x01": "x00";
if ($palette _size) {
$GD _header. = Pack ("n", $ncolor);
}
No transparency
$GD _header. = "Xffxffxffxff";

Fwrite ($dest _f, $gd _header);

if ($palette _size) {
$palette = Fread ($src _f, $palette _size);
$GD _palette = "";
$j = 0;
while ($j < $palette _size) {
$b = $palette {$j + +};
$g = $palette {$j + +};
$r = $palette {$j + +};
$a = $palette {$j + +};
$GD _palette. = "$r $g$b$a";
}
$GD _palette. = Str_repeat ("x00x00x00x00", 256-$ncolor);
Fwrite ($dest _f, $gd _palette);
}

$scan _line_size = (($bits * $width) + 7) >> 3;
$scan _line_align = ($scan _line_size & 0x03)? 4-($scan _line_size &
0X03): 0;

for ($i = 0, $l = $height-1; $i < $height; $i + +, $l-) {
BMP stores scan lines starting from bottom
Fseek ($src _f, $offset + (($scan _line_size + $scan _line_align) * $l));
$scan _line = fread ($src _f, $scan _line_size);
if ($bits = = 24) {
$GD _scan_line = "";
$j = 0;
while ($j < $scan _line_size) {
$b = $scan _line{$j + +};
$g = $scan _line{$j + +};
$r = $scan _line{$j + +};
$GD _scan_line. = "X00$r$g$b";
}
}
else if ($bits = = 8) {
$GD _scan_line = $scan _line;
}
else if ($bits = = 4) {
$GD _scan_line = "";
$j = 0;
while ($j < $scan _line_size) {
$byte = Ord ($scan _line{$j + +});
$p 1 = chr ($byte >> 4);
$p 2 = Chr ($byte & 0x0F);
$GD _scan_line. = "$p 1$p2";
}
$GD _scan_line = substr ($gd _scan_line, 0, $width);
}
else if ($bits = = 1) {
$GD _scan_line = "";
$j = 0;
while ($j < $scan _line_size) {
$byte = Ord ($scan _line{$j + +});
$p 1 = chr ((int) (($byte & 0x80)! = 0));
$p 2 = chr ((int) (($byte & 0x40)! = 0));
$p 3 = chr ((int) (($byte & 0x20)! = 0));
$p 4 = chr ((int) (($byte & 0x10)! = 0));
$p 5 = chr ((int) (($byte & 0x08)! = 0));
$p 6 = chr ((int) (($byte & 0x04)! = 0));
$p 7 = chr ((int) (($byte & 0x02)! = 0));
$p 8 = chr ((int) (($byte & 0x01)! = 0));
$GD _scan_line. = "$p 1$p2$p3$p4$p5$p6$p7$p8";
}
$GD _scan_line = substr ($gd _scan_line, 0, $width);
}
Fwrite ($dest _f, $gd _scan_line);
}
Fclose ($src _f);
Fclose ($dest _f);
return true;
}

function Imagecreatefrombmp ($filename) {
$tmp _name = Tempnam ("/tmp", "GD");
if ($this->convertbmp2gd ($filename, $tmp _name)) {
$img = IMAGECREATEFROMGD ($tmp _name);
Unlink ($tmp _name);
return $img;
}
return false;
}

}

http://www.bkjia.com/PHPjc/632981.html www.bkjia.com true http://www.bkjia.com/PHPjc/632981.html techarticle 1. From which we can see the role of the Imagecreatetruecolor function is obviously to create a black background image, its first parameter is the width of the created picture, the second parameter is the created diagram ...

  • 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.