PHP thumbnails generate several method codes

Source: Internet
Author: User
Tags fread image identifier imagejpeg ord pack php tutorial strlen unpack

1. From which we can see that the function of the imagecreatetruecolor is obviously to create a black background picture, its first parameter is the width of the created picture, the second parameter is the height of the created picture, we put the return value (image identifier) of this function into the variable.

2.imagecreatefromjpeg function is to be divided into the image of the picture read into memory (here you may have the question: I directly from the hard micro-disk read not to get, why do I have to read the memory? For an improper analogy, People usually in the money when we believe that people will not put too much in their pockets, 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 image to be divided or other operations to read it into memory, plainly, Memory provides a running stage for the program)

3. The function of the imagecopyresampled function is to split the original image, and then copy it and sample copies (which I understand as projections) to the background picture created with Imagecreatefromjpeg.

<?php Tutorial
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;

Create a new picture with a width of 0.5 of the original picture 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), split the length (400), height (300) Half of the original picture, and place the segmented picture in the established area from the coordinates (0,0).
Imagecopyresampled ($image _p, $image, 0, 0, +, $new _width, $new _height, 400, 300);

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

The above example is the $image picture from the coordinates (100,100), the split width of 400, high 300, and then the picture from the coordinates (0,0) from the beginning of the projection to the picture $image_p, the projection of the width of $new_width, high for the $new_ Height

<?php

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

&lt;?php


/* 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-Image Save height,


$rate-Image 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 &gt; $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 &lt;0) 0: $dstX;


$dstY = ($dstX &lt;0) 0: $dstY;


$dstX = ($dstX &gt; ($dstW/2)) Floor ($dstW/2): $dstX;


$dstY = ($dstY &gt; ($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


Parameters in order, Text size | deflection | horizontal Axis | 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 image watermark, watermark picture size "&gt; Picture size defaults to 88*31


Imagedestroy ($wimage);


}


Imagejpeg ($ni, $dstFile, $rate);


Imagejpeg ($ni, $srcFile, $rate);


Imagedestroy ($im);


Imagedestroy ($ni);


}


?&gt;

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 {

Picture type
var $type;

Actual width
var $width;

Actual height
var $height;

The width after the change
var $resize _width;

Change the height of the post
var $resize _height;

Whether or not to cut the map
var $cut;

Source image
var $srcimg;

Target Image Address
var $dstimg;

Temporarily created image
var $im;

function Resizeimage ($img, $wid, $hei, $c, $dstpath) {


$this-&gt;srcimg = $img;


$this-&gt;resize_width = $wid;


$this-&gt;resize_height = $hei;


$this-&gt;cut = $c;





Type of picture


$this-&gt;type = Strtolower (substr (STRRCHR ($this-&gt;srcimg, "."), 1);





Initializing images


$this-&gt;initi_img ();





Target Image Address


$this-&gt; dst_img ($dstpath);





//--


$this-&gt;width = imagesx ($this-&gt;im);


$this-&gt;height = Imagesy ($this-&gt;im);





Build image


$this-&gt;newimg ();





Imagedestroy ($this-&gt;im);


}

function newimg () {

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

The proportions of the actual image
$ratio = ($this->width)/($this->height);

if (($this-&gt;cut) = = "1") {


High priority in drawing


if ($ratio &gt;= $resize _ratio) {


$newimg = Imagecreatetruecolor ($this-&gt;resize_width, $this-&gt;resize_height);


Imagecopyresampled ($newimg, $this-&gt;im, 0, 0, 0, 0, $this-&gt;resize_width, $this-&gt;resize_height, ($this-&gt; Height) * $resize _ratio), $this-&gt;height);


Imagejpeg ($newimg, $this-&gt;dstimg);


}





Chart Width First


if ($ratio &lt; $resize _ratio) {


$newimg = Imagecreatetruecolor ($this-&gt;resize_width, $this-&gt;resize_height);


Imagecopyresampled ($newimg, $this-&gt;im, 0, 0, 0, 0, $this-&gt;resize_width, $this-&gt;resize_height, $this-&gt;width , (($this-&gt;width)/$resize _ratio));


Imagejpeg ($newimg, $this-&gt;dstimg);


}


} else {


Do not cut the figure


if ($ratio &gt;= $resize _ratio) {


$newimg = Imagecreatetruecolor ($this-&gt;resize_width, ($this-&gt;resize_width)/$ratio);


Imagecopyresampled ($newimg, $this-&gt;im, 0, 0, 0, 0, $this-&gt;resize_width, ($this-&gt;resize_width)/$ratio, $this- &gt;width, $this-&gt;height);


Imagejpeg ($newimg, $this-&gt;dstimg);


}


if ($ratio &lt; $resize _ratio) {


$newimg = Imagecreatetruecolor ($this-&gt;resize_height) * $ratio, $this-&gt;resize_height);


Imagecopyresampled ($newimg, $this-&gt;im, 0, 0, 0, 0, ($this-&gt;resize_height) * $ratio, $this-&gt;resize_height, $ This-&gt;width, $this-&gt;height);


Imagejpeg ($newimg, $this-&gt;dstimg);


}


}


}

Initializing images


function initi_img () {


if ($this-&gt;type== "jpg") {


$this-&gt;im = imagecreatefromjpeg ($this-&gt;srcimg);


}





if ($this-&gt;type== "gif") {


$this-&gt;im = imagecreatefromgif ($this-&gt;srcimg);


}





if ($this-&gt;type== "png") {


$this-&gt;im = imagecreatefrompng ($this-&gt;srcimg);


}





if ($this-&gt;type== "bmp") {


$this-&gt;im = $this-&gt;imagecreatefrombmp ($this-&gt;srcimg);


}


}

Image Destination Address


function Dst_img ($dstpath) {


$full _length = strlen ($this-&gt;srcimg);


$type _length = strlen ($this-&gt;type);


$name _length = $full _length-$type _length;


$name = substr ($this-&gt;srcimg,0, $name _length-1);


$this-&gt;dstimg = $dstpath;


Echo $this-&gt;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 &lt; $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 &lt; $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 &lt; $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 &lt; $scan _line_size) {


$byte = Ord ($scan _line{$j + +});


$p 1 = chr ($byte &gt;&gt; 4);


$p 2 = Chr ($byte &amp; 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 &lt; $scan _line_size) {


$byte = Ord ($scan _line{$j + +});


$p 1 = chr ((int) (($byte &amp; 0x80)!= 0));


$p 2 = chr ((int) (($byte &amp; 0x40)!= 0));


$p 3 = chr ((int) (($byte &amp; 0x20)!= 0));


$p 4 = chr ((int) (($byte &amp; 0x10)!= 0));


$p 5 = chr ((int) (($byte &amp; 0x08)!= 0));


$p 6 = chr ((int) (($byte &amp; 0x04)!= 0));


$p 7 = chr ((int) (($byte &amp; 0x02)!= 0));


$p 8 = chr ((int) (($byte &amp; 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;
}

}

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.