Php imagecreate instance tutorial

Source: Internet
Author: User

Php imagecreate instance tutorial

Description
Resource imagecreate ($ width, $ height)
Imagecreate () returns an image identifier representing a blank image of the specified size.

We recommend that you use imagecreatetruecolor ().

Check items
<? Php
Header ("Content-type: image/png ");
$ Im = @ imagecreate (110, 20)
Or die ("Cannot Initialize new GD image stream ");
$ Background_color = imagecolorallocate ($ im, 0, 0, 0 );
$ Text_color = imagecolorallocate ($ im, 233, 14, 91 );
Imagestring ($ im, 1, 5, 5, "A Simple Text String", $ text_color );
Imagepng ($ im );
Imagedestroy ($ im );
?>

If you can access gd. h (rarely on a shared host), you can modify GD_RESOLUTION and give it the required value.

In this solution, I found that the JPEG header bytes are directly manipulated.
I hope this sample code will help others, because I spent 5 hours digging to speculate that I was leaving the system and there was no other choice.

<? Php
Header ('content-Disposition: attachment; filename = "myimg.jpg "');
Header ('cache-Control: private ');
$ Dst = imagecreatetruecolor (300,300);/* this creates a 300x300 pixels image */
Ob_start ();/* don't send the output to the browser since we'll need to manipulate it */
ImageJpeg ($ dst );
$ Img = ob_get_contents ();
Ob_end_clean ();

// Byte #14 in jpeg sets the resolution type: 0 = none, 1 = pixels per inch, 2 = pixels per cm
// Bytes 15-16 sets X resolution
// Bytes 17-18 sets Y resolution
$ Img = substr_replace ($ img, pack ('CNN ', 1,300,300), 13, 5 );

Echo $ img;

?>

The file is in its file type and returns a false load if it fails
<? Php
Function imagecreatefromfile ($ path, $ user_functions = false)
{
$ Info = @ getimagesize ($ path );
   
If (! $ Info)
    {
Return false;
    }
   
$ Functions = array (
IMAGETYPE_GIF => 'imagecreatefromgif ',
IMAGETYPE_JPEG => 'imagecreatefromjpeg ',
IMAGETYPE_PNG => 'imagecreatefrompng ',
IMAGETYPE_WBMP => 'imagecreatefromwbmp ',
IMAGETYPE_XBM => 'imagecreatefromwxbm ',
);
   
If ($ user_functions)
    {
$ Functions [IMAGETYPE_BMP] = 'imagecreatefrombmp ';
    }
   
If (! $ Functions [$ info [2])
    {
Return false;
    }
   
If (! Function_exists ($ functions [$ info [2])
    {
Return false;
    }
   
Return $ functions [$ info [2] ($ path );
}
?>

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.