Creating images with php

imagecreate

imagecreate() returns an image identifier representing a blank image of specified size.

In general, we recommend the use of imagecreatetruecolor() instead of imagecreate() so that image processing occurs on the highest quality image possible. If you want to output a palette image, then imagetruecolortopalette() should be called immediately before saving the image with imagepng() or imagegif() .

Parameters

Return Values

Returns an image object on success, false on errors.

Changelog

Version Description
8.0.0 On success, this function returns a GDImage instance now; previously, a resource was returned.

Examples

Example #1 Creating a new GD image stream and outputting an image.

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

The above example will output something similar to:

See Also

User Contributed Notes 20 notes

to create an image from a BMP file, I made this function, that return a resource like the others ImageCreateFrom function:

function ImageCreateFromBMP ( $filename )
//Ouverture du fichier en mode binaire
if (! $f1 = fopen ( $filename , «rb» )) return FALSE ;

//1 : Chargement des ent?tes FICHIER
$FILE = unpack ( «vfile_type/Vfile_size/Vreserved/Vbitmap_offset» , fread ( $f1 , 14 ));
if ( $FILE [ ‘file_type’ ] != 19778 ) return FALSE ;

//2 : Chargement des ent?tes BMP
$BMP = unpack ( ‘Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel’ .
‘/Vcompression/Vsize_bitmap/Vhoriz_resolution’ .
‘/Vvert_resolution/Vcolors_used/Vcolors_important’ , fread ( $f1 , 40 ));
$BMP [ ‘colors’ ] = pow ( 2 , $BMP [ ‘bits_per_pixel’ ]);
if ( $BMP [ ‘size_bitmap’ ] == 0 ) $BMP [ ‘size_bitmap’ ] = $FILE [ ‘file_size’ ] — $FILE [ ‘bitmap_offset’ ];
$BMP [ ‘bytes_per_pixel’ ] = $BMP [ ‘bits_per_pixel’ ]/ 8 ;
$BMP [ ‘bytes_per_pixel2’ ] = ceil ( $BMP [ ‘bytes_per_pixel’ ]);
$BMP [ ‘decal’ ] = ( $BMP [ ‘width’ ]* $BMP [ ‘bytes_per_pixel’ ]/ 4 );
$BMP [ ‘decal’ ] -= floor ( $BMP [ ‘width’ ]* $BMP [ ‘bytes_per_pixel’ ]/ 4 );
$BMP [ ‘decal’ ] = 4 -( 4 * $BMP [ ‘decal’ ]);
if ( $BMP [ ‘decal’ ] == 4 ) $BMP [ ‘decal’ ] = 0 ;

//3 : Chargement des couleurs de la palette
$PALETTE = array();
if ( $BMP [ ‘colors’ ] < 16777216 )
$PALETTE = unpack ( ‘V’ . $BMP [ ‘colors’ ], fread ( $f1 , $BMP [ ‘colors’ ]* 4 ));
>

//4 : Cr?ation de l’image
$IMG = fread ( $f1 , $BMP [ ‘size_bitmap’ ]);
$VIDE = chr ( 0 );

$res = imagecreatetruecolor ( $BMP [ ‘width’ ], $BMP [ ‘height’ ]);
$P = 0 ;
$Y = $BMP [ ‘height’ ]- 1 ;
while ( $Y >= 0 )
$X = 0 ;
while ( $X < $BMP [ 'width' ])
if ( $BMP [ ‘bits_per_pixel’ ] == 24 )
$COLOR = unpack ( «V» , substr ( $IMG , $P , 3 ). $VIDE );
elseif ( $BMP [ ‘bits_per_pixel’ ] == 16 )
<
$COLOR = unpack ( «n» , substr ( $IMG , $P , 2 ));
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ]+ 1 ];
>
elseif ( $BMP [ ‘bits_per_pixel’ ] == 8 )
<
$COLOR = unpack ( «n» , $VIDE . substr ( $IMG , $P , 1 ));
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ]+ 1 ];
>
elseif ( $BMP [ ‘bits_per_pixel’ ] == 4 )
$COLOR = unpack ( «n» , $VIDE . substr ( $IMG , floor ( $P ), 1 ));
if (( $P * 2 )% 2 == 0 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] >> 4 ) ; else $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x0F );
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ]+ 1 ];
>
elseif ( $BMP [ ‘bits_per_pixel’ ] == 1 )
$COLOR = unpack ( «n» , $VIDE . substr ( $IMG , floor ( $P ), 1 ));
if (( $P * 8 )% 8 == 0 ) $COLOR [ 1 ] = $COLOR [ 1 ] >> 7 ;
elseif (( $P * 8 )% 8 == 1 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x40 )>> 6 ;
elseif (( $P * 8 )% 8 == 2 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x20 )>> 5 ;
elseif (( $P * 8 )% 8 == 3 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x10 )>> 4 ;
elseif (( $P * 8 )% 8 == 4 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x8 )>> 3 ;
elseif (( $P * 8 )% 8 == 5 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x4 )>> 2 ;
elseif (( $P * 8 )% 8 == 6 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x2 )>> 1 ;
elseif (( $P * 8 )% 8 == 7 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x1 );
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ]+ 1 ];
>
else
return FALSE ;
imagesetpixel ( $res , $X , $Y , $COLOR [ 1 ]);
$X ++;
$P += $BMP [ ‘bytes_per_pixel’ ];
>
$Y —;
$P += $BMP [ ‘decal’ ];
>

//Fermeture du fichier
fclose ( $f1 );

Источник

How to generate an image: using PHP GD Library. (part 1)

photo by Christiana Morillo from Pexels: https://www.pexels.com/photo/close-up-photo-of-person-typing-on-laptop-1181675/

PHP GD is a library that performs fantastic tasks that helps you to do several things with your image. As a PHP Developer, There are times when you have to survey into some advance functionality of your stack. And one is the GD library, is not easily done by most developers.
and it helps you do anything with image. If you’re ready, let’s proceed to this PHP amazing library that helps you generate, and draw an image.

What is GD Library

GD is an acronym that stands for “GRAPHIC DRAW”. The GD library in PHP enables you to create image data, and it’s the most used image processing tool in PHP. GD was originally developed by Thomas Boutell. By far it is a powerful image-processing library for PHP (it is also available for other languages like Perl). GD library allows you to dynamically create and edit of images, charts thumbnails, etc. The following file type that can be created or edited using the GD library, are GIF, JPEG, and PNG.

How Does GD Library work

One thing you should understand is that the GD library has a pattern in which it works. Now if you don’t follow the process of the GD library you won’t see anything on your interface. Below are the steps for implementing GD library in your project: Step 1:
You must check if the GD library is enabled in your system local server (XAMMP). If it’s disabled you have to enable it, once it’s enabled you are good to go. If yours is enabled you will see a display similar to the image below. To see if the GD library is enabled in your local server, go to your PHP index file and echo **phpinfo();** then you’ll see the GD support in your interface. little example, from (me) Rabbi Emmykolic Step 2:
You will go to your **index.php** and create an image tag. And in the src of the image tag, you’ll write the address of the PHP file where you want to write the GD codes. So the image tag will look like this. An Example From (me) Rabbi Emmykolic Now, this is where you’ll write the PHP code. little example, from (me) Rabbi Emmykolic

After this step, you’ll start writing some functions of the GD library. In the PHP file your GD will start working automatically.

Using GD Library in your projects

Example *****1:* Using GD Library to create a Color-box with a JPEG extension, when saved. *****Step1:* Create an image with the specified dimensions

Step2: Create a color (this first call to imageColorAllocate also automatically sets the image background color)

 $colorRed = imageColorAllocate($image, 255,0,0); 
$colorYellow = imageColorAllocate($image, 255,255,0); 
imageFilledRectangle($image, 50, 50, 250, 150, $colorYellow); 
header("Content-type: image/jpeg"); imageJpeg($image); 

little example, from (me) Rabbi Emmykolic

By running the above code, you will get the output seen in the image below: Example *****2:* How to use GD library to create and rotate a png image. Run the code below to see how to rotate an image. You’ll have to put an image to the PHP file you’re working with, else it won’t dispaly on your interface.

// Create an image with the specified dimensions $image = imagecreatefrompng("africa_coin.png");//and the name of the image with specified extension. // Create a color $colorYellow = imageColorAllocate($image, 255,255,0); // Rotate $image=imagerotate($image, 60, $colorYellow); // Set type of image and send the output header("Content-type: image/png"); imagePng($image); // Release memory imageDestroy($image); 

little example, from (me) Rabbi Emmykolic

By running the above code, you will get the output below:

Why you should use GD Library

GD is an advanced PHP, you can agree with me often advanced PHP developers are the one using GD library. So if you want to be a king in the PHP realm you have to use stuff like this. Another reason you should use GD library is that it helps you do dynamic things with images, in your PHP files. Lastly, It saves you the stress of writing lots of JavaScript, CSS & HTML code…

Other things GD Library can be used for

  • You can use the GD library to build a Captcha system.
  • You can use the GD library to reduce the quality of an image. ## How to generate an image with GD Library

The way to generate images in GD is pretty simple if you have used it before. The first step to making an image to show is to use the **ImageCreate()** function. Then specify the width and height. But if you want to create an image with a png extension you’ll have to use the **ImageCreatePng()** function.

The functions mentioned above, are few listed function that can be used to create a GD library. so the pics below shows some of those function.

little example, from (me) Rabbi Emmykolic

Conclusion

We’ve come to the end of this tutorial, hopefully you got some ideas from the teaching. From the tutorial, we were able to bring to generate an png and jpg images using the GD library.

This is not the end of the tutorial, we’ll talk more about how to use GD to make other things.

About the Author

Emmanuel C. Okolie kick-started his journey as a software engineer in 2020. Over the years, he has grown full-blown skills in JavaScript, PHP, HTML & CSS, and more.

He is freelancing, building clients’ websites, and writing technical tutorials teaching others how to do what he does.

Emmanuel C. Okolie is open and available to hear from you. You can reach him on Linked In, Google, Facebook, GitHub, Twitter, or on his website.

Источник

Читайте также:  Top 100 menu html
Оцените статью