Php thumbnails generation method code _ PHP Tutorial

Source: Internet
Author: User
Tags crop image image identifier imagejpeg
Php thumbnails generate several code methods. 1. the imagecreatetruecolor function is used to create a black background image. The first parameter is the width of the created image, and the second parameter is the created image. the imagecreatetruecolor function is used to create a black background image. Its first parameter is the width of the created image, the second parameter is the height of the created image. we store the return value (image identifier) of this function into the variable.

2. imagecreatefromjpeg is used to read the image to be split into memory (here we may have doubts: I can't read it directly from the hard disk. why should I read it first? For example, when you use money, I believe that you will not put too much in your pockets. Generally, you will get it from the bank when you use it. The same is true here, when this image is not used, I put it in the hard disk. when I want to split the image or perform other operations, I read it into the memory. to put it bluntly, memory provides a stage for the program to run)

3. now let's look at the imagecopyresampled function. it splits the original image and copies it from the sample image (I think it is a projection) to the background image created 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;

// Use 0.5 of the original image's length and width as the new length and width to create a new image. the logo of this image is $ image_p.
$ Image_p = imagecreatetruecolor ($ new_width, $ new_height );
// Create an image from a JPEG file or URL
$ Image = imagecreatefromjpeg ($ filename );
// Split the original image from the coordinate (100,100). The split length is (400) and the height is (300) half of the original image. Place the split image in the slave coordinate) in the first region
Imagecopyresampled ($ image_p, $ image, 0, 0,100,100, $ new_width, $ new_height, 400,300 );

// Output
Imagejpeg ($ image_p, null, 100); // quality indicates that the quality of the image output ranges from 0 (worst quality, smaller file size) to 100 (best quality, maximum file size ).
?>

In the above example, the $ image is split from the coordinate (100,100), the width after the split is 400, the height is 300, and then the image is taken from the coordinate (0, 0) start to project the image to $ image_p. the projection width is $ new_width and the height is $ new_height.

// File and zoom size
// $ 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 tutorial

/* Constructor-generate thumbnails + watermarks. parameter description:
$ SrcFile-image file name,
$ DstFile-save another file name,
$ Markwords-watermark text,
$ Markimage-watermark image,
$ DstW-Image save width,
$ Dsomething-image storage height,
$ Rate-image storage quality */
Makethumb ("a.jpg", "B .jpg", "50", "50 ");
Function makethumb ($ srcFile, $ dstFile, $ dstW, $ dsomething, $ 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 * $ DTH> $ srcH * $ dstW)
{
$ Fd…… = round ($ srcH * $ dstW/$ srcW );
$ DstY = floor ($ DTH-$ fdth)/2 );
$ FdstW = $ dstW;
}
Else
{
$ FdstW = round ($ srcW * $ dsomething/$ srcH );
$ DstX = floor ($ dstW-$ fdstW)/2 );
$ Fdth = $ DTH;
}
$ Ni = ImageCreateTrueColor ($ dstW, $ dsomething );
$ DstX = ($ dstX <0 )? 0: $ dstX;
$ DstY = ($ dstX <0 )? 0: $ dstY;
$ DstX = ($ dstX> ($ dstW/2 ))? Floor ($ dstW/2): $ dstX;
$ DstY = ($ dstY> ($ dsomething/2 ))? Floor ($ dsomething/s): $ dstY;
$ White = ImageColorAllocate ($ ni, 255,255,255 );
$ Black = ImageColorAllocate ($ ni, 0, 0 );
Imagefilledrectangle ($ ni, $ dstW, $ DTH, $ white); // fill the background color
ImageCopyResized ($ ni, $ im, $ dstX, $ dstY, 0,0, $ fdstW, $ fdsomething, $ srcW, $ srcH );
If ($ markwords! = Null)
{
$ Markwords = iconv ("gb2312", "UTF-8", $ markwords );
// Convert the text encoding
ImageTTFText ($ ni, 20, 30, 450,560, $ black, "simhei. ttf", $ markwords); // write a text watermark
// The parameters are in sequence: text size | deflection degree | abscissa | 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, 88,31); // write the image watermark, watermark image size "> the default image size is 88*31.
Imagedestroy ($ wimage );
}
ImageJpeg ($ ni, $ dstFile, $ rate );
ImageJpeg ($ ni, $ srcFile, $ rate );
Imagedestroy ($ im );
Imagedestroy ($ ni );
}
?>

Instance 4

$ Thumbnail = new ImageResize ();
$ Thumbnail-> resizeimage (source Image full path, thumbnail width, thumbnail height, whether to crop (0 or 1), New Image full path );

Class ImageResize {

// Image type
Var $ type;

// Actual width
Var $ width;

// Actual height
Var $ height;

// Change the width
Var $ resize_width;

// The height after the change
Var $ resize_height;

// Whether to cut the image
Var $ cut;

// Source image
Var $ srcimg;

// Target Image address
Var $ dstimg;

// Temporarily created image
Var $ im;

Function resizeimage ($ img, $ wid, $ hei, $ c, $ dstpath ){
$ This-> srcimg = $ img;
$ This-> resize_width = $ wid;
$ This-> resize_height = $ hei;
$ This-> cut = $ c;

// Image type
$ This-> type = strtolower (substr (strrchr ($ this-> srcimg, "."), 1 ));

// Initialize the image
$ This-> initi_img ();

// Target Image address
$ This-> dst_img ($ dstpath );

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

// Generate an image
$ This-> newimg ();

ImageDestroy ($ this-> im );
}

Function newimg (){

// Ratio of the changed image
$ Resize_ratio = ($ this-> resize_width)/($ this-> resize_height );

// Ratio of the actual image
$ Ratio = ($ this-> width)/($ this-> height );

If ($ this-> cut) = "1 "){
// Image cutting 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 );
}

// Crop Image 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 {
// No cut Chart
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 );
}
}
}

// Initialize the 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 );
}
}

// Target Image 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 $ ";
}
$ 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 ++ });
$ P1 = chr ($ byte> 4 );
$ P2 = chr ($ byte & 0x0F );
$ Gd_scan_line. = "$ p1 $ 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 ++ });
$ P1 = chr (int) ($ byte & 0x80 )! = 0 ));
$ P2 = chr (int) ($ byte & 0x40 )! = 0 ));
$ P3 = chr (int) ($ byte & 0x20 )! = 0 ));
$ P4 = chr (int) ($ byte & 0x10 )! = 0 ));
$ P5 = chr (int) ($ byte & 0x08 )! = 0 ));
$ P6 = chr (int) ($ byte & 0x04 )! = 0 ));
$ P7 = chr (int) ($ byte & 0x02 )! = 0 ));
$ P8 = chr (int) ($ byte & 0x01 )! = 0 ));
$ Gd_scan_line. = "$ p1 $ 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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632981.htmlTechArticle1. the imagecreatetruecolor function is used to create a black background image. Its first parameter is the width of the created image, the second parameter is the created image...

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.