PHP Image Processing-PNG transparent scaling and Gray Image Generation

Source: Internet
Author: User

Not to mention nonsense.Code:

 

If (move_uploaded_file ($ arrfile ['tmp _ name'], $ strpicname )){

Switch ($ fileext ){

Case ". GIF": // GIF

$ Im = imagecreatefromgif ($ strpicname );

Break;

Case ". PNG": // PNG

$ Im = imagecreatefrompng ($ strpicname );

Break;

Default: // JPG

$ Im = imagecreatefromjpeg ($ strpicname );

Break;

}

// Obtain image information

$ Imageinfo = $ this-> getinfo ($ strpicname );

 

// Create a new canvas for zooming and transparency

If (function_exists ("imagecreatetruecolor") {// gd2.0.1

$ New = imagecreatetruecolor (150, 80 );

$ This-> addtranbylistimage ($ fileext, $ new );

Imagecopyresampled ($ new, $ im, 0, 0, 0, 0,150, 80, $ imageinfo ["width"], $ imageinfo ["height"]);

} Else {

$ New = imagecreate (150, 80 );

$ This-> addtranbylistimage ($ fileext, $ new );

Imagecopyresized ($ new, $ im, 0, 0, 0, 0,150, 80, $ imageinfo ["width"], $ imageinfo ["height"]);

}

// Generate a thumbnail

Switch ($ fileext ){

Case ". GIF": // GIF

Imagegif ($ new, $ strpicname );

Break;

Case ". PNG": // PNG

Imagepng ($ new, $ strpicname );

Break;

Default: // JPG

Imagejpeg ($ new, $ strpicname );

Break;

}

// Generate a gray chart

If ($ New & imagefilter ($ new, img_filter_grayscale )){

Imagepng ($ new, $ strpicname. "_grey.png ");

Return $ strphoto;

} Else {

Check: alertexit ('Gray Graph Generation failed! ',-1 );

}

} Else {

Check: alertexit ($ strpicname. 'file upload error! ',-1 );

}

 

The above code will not be re-written. We will start from move_uploaded_file.

After the file is uploaded, get the $ im object according to the file path, that is, the imagecreatexxx object. $ Fileext is the suffix of the obtained image. Other content annotations should also be clearly written. Let's take a look at these two methods: $ this-> getinfo ($ strpicname); and $ this-> addtranbylistimage ($ fileext, $ new );

 

$ This-> getinfo (); actually, it's easy to get the image information:

 

Function getinfo ($ file ){

$ DATA = getimagesize ($ file );

$ Imageinfo ["width"] = $ data [0];

$ Imageinfo ["height"] = $ data [1];

$ Imageinfo ["type"] = $ data [2];

$ Imageinfo ["name"] = basename ($ file );

Return $ imageinfo;

}

 

$ This-> addtranbylistimage () is our focus today:

 

Function addtranbylistimage ($ fileext, $ IMG ){

Switch ($ fileext ){

Case ". GIF": // GIF

Case ". PNG": // PNG

Imagealphablending ($ IMG, true );

Imagesavealpha ($ IMG, true );

$ Trans_colour = imagecolorallocatealpha ($ IMG, 0, 0, 0,127 );

Imagefill ($ IMG, 0, 0, $ trans_colour );

Break;

Default: // JPG

Break;

}

}

 

In fact, it is also a very simple method. I just found it on Baidu and added itProgramAnd the test is completed. Under normal circumstances, after the uploaded transparent PNG image with a background is scaled or some operations are performed, the transparent background of the image becomes black, the Code is the key statement to ensure that the background is still transparent.

 

As for the gray image, the imagefilter method is simpler. The following parameters are all constants.

 

------------------------------------------------

If you use word2007 to publish an image, the image is not displayed, or you are prompted to have an attachment but no attachment, go to the independent blog to view the complete information.Article!

Modify and add content of the article only in the independent blog!

My independent blog: kiddie-Http://www.zyblog.net/

Link to this article:Http://www.zyblog.net/post-83.html

You are welcome to reprint it. Please indicate the source of this article.

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.