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 );
}
?>