PHP Creating Image

In PHP there have many predefined function which helps to create background image and write text in the image.

Like create CAPTCHA image for website. 





Firstly if user write any image code in PHP script need to set content-type:image/jpeg.

Because by default PHP script content type is text/html. Browser only read text value so if user display image using PHP then header(“Content-Type: image/jpeg”) must be set at the top of the code.

In this way whole PHP file content set for image not text.

If any user forget to set header(content-type:) then browser return image as a unreadable text

  1. imagecreatetrucolor(width,height) is a PHP function which create a background image with black color.400, 70 is the width and height value of this background image. All parameters are required.
  2. imagecolorallocate(backgroundimage,red,green,blue) is a PHP function which set the color value through RGB(Red, Green, Blue) format.    First parameter is the background field and second parameter is red value (0,255) third parameter is green value (0,255) and fourth parameter is blue value (0,255).In the above example red value is 52, green value is 237 and blue value is 214. All parameters are required.
  3. imagefilledrectangle(backgroundimage,x-coordinator,y-coordinator,width,height,color) this function is return the rectangle field on the black background image.The first parameter is background image value, the second parameter is X-coordinator value means according to X-axis where the rectangle field is create on the background image, third parameter is Y-coordinator value means according to Y-axis where the rectangle field is create on the background image, forth parameter is width value of the rectangle field, fifth parameter is height value of the rectangle field and sixth parameter is color value of the rectangle field. All parameters are required.
  4. imagettftext(backgroundimage,textfontwidth,textfontangle, x-coordinator, y-coordinator,textfontcolor,textfontstyle,textvalue) this function write any text value on the image. All parameters are required.
    1. backgroundimagewhere text value is print.
    2. textfontwidth – text font width. In the above example font width is 30px.
    3. textfontangletext font angle means which angle text font display on the image. User can give positive value and also negative value. In the above example no angle is use so put 0 in this parameter.
    4. x-coordinator – x-axis’s position of the font. In the above example x-axis is 80
    5. y-coordinator – y-axis’s position of the font. In the above example y-axis is 50
    6. textfontcolortext color means text will be display on the image with which color. Color value set by imagecolorallocate() function. In the above example use 254,7,82 RGB value.
    7. textfontstyleuse font file for text font style. Download external font or copy windows font in the project location.
    8. In the above example use windows font and rename it with 4.ttf
    9. textvaluethe original value which user want to write on the image. In the above example “PHP is Amazing”.
  5. imagejpeg($image) this is the function which return the image in the browser. If user save the image in the directory then use imagejpeg($image, “image.jpg”). After execute this code one image file save in the project location with the name image.jpg

Using above five functions user can create any image file in web.

One application example

PHP image creator


Image resize by PHP

A website mainly creates by images and contents. Website is a virtual things where real things display by images. So if any website has lots of high quality images then the site load very slowly even net speed is high. When anyone open any site first and quickly display content then images and then videos if there. So it’s very important create a website light weight but images are same high resolution.

Using some PHP function easily resizes images without changing the original resolution of the image by changing the image original width height.

And also can resize image without change height width by changing the resolution of the image.



Return new.jpg image with new width 250px and new height 250px and the file size is 9.60kb which is very small than predefinedoriginal image size.


imagecreatefromjpeg(‘image file location’) this function is used to receive original image.

In the above example “Desert.jpg” is the original image with 1024width 768height. The image size is 826kb. $img variable is store “Desert.jpg” image.

If the image file extension is change from jpeg or jpg to png or gif then function name also change according to file. Like use imagecreatefrompng() if image is png image. use imagecreatefromgif() if image is gif image.

imagecreatetruecolor(width, height) create a background with allocate width and height value and black color.

imagecopyresized(new image value, original image value, new image x-axis value, new image y-axis value, original image x-axis value, original image y-axis value, new image width, new image height, original image width, original image height). All parameters are required.

This function resizes the image according to new width and height value.

imagejpg(new image value, name of new image). This function returns new jpeg or jpg image in the project location.

If user wants to return png image then the function is change with predefined()   same as gif image use the ().

PHP Image Compression

Resize image without changing width and height.

Note:70 is the resolution quality of the image. 0 to 100 is the value of this parameter.  0 is low quality image and 100 is the high quality image.

By changing the resolution quality can be change the image original size.