In PHP there have many
Like create CAPTCHA image for website.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php header("Content-Type: image/jpeg"); // Set the content type header - in this case image/jpeg $image = imagecreatetruecolor(400,70); $color = imagecolorallocate($image,52,237,214); imagefilledrectangle($image,0,0,400,70,$color); $fontstyle = "4.ttf"; $fontcolor = imagecolorallocate($image,254,7,82); $text = "PHP is Amazing"; imagettftext($image,30,0,80,50,$fontcolor,$fontstyle,$text); imagejpeg($image); imagejpeg($image, "image.jpg"); ?> |
Output
Explanation:
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/
In this way whole PHP file content set for
If any user forget to set header(content-type:) then browser return image as
- 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.
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 aboveexample red value is 52,green value is 237 andblue value is 214. All parameters are required.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 fieldis create on the background image, third parameter is Y-coordinator value means according to Y-axis where the rectangle fieldis 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.imagettftext (backgroundimage ,textfontwidth ,textfontangle , x-coordinator, y-coordinator,textfontcolor ,textfontstyle ,textvalue ) this function write any text value on the image. All parameters are required.backgroundimage – where text value is print.- textfontwidth – text font width. In the above example font width is 30px.
textfontangle – text font angle means which angle text font display on the image.User can give positive value and alsonegative value. In the aboveexample no angleis use so put 0 in this parameter.- x-coordinator – x-axis’s position of the font. In the above example x-axis is 80
- y-coordinator – y-axis’s position of the font. In the above
example y-axis is 50 textfontcolor – text color means text will be display on the image with which color. Color value set byimagecolorallocate () function. In the above example use 254,7,82 RGB value.textfontstyle – use font file for text font style. Download external font or copy windows font in the project location.- In the above example use windows font and rename it with 4.ttf
textvalue – the original value which user want to write on the image. In the above example “PHP is Amazing”.
- imagejpeg($image) this is the function which
return the image in the browser. If usersave the image in the directory then useimagejpeg ($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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php header("Content-Type:image/jpeg"); $image = imagecreatetruecolor(500,80); $color = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); imagefilledrectangle($image,0,0,500,80,$color); /*//user can use any readymate background picture by $image = imagecreatefrompng("background.png");*/ $x = 0; for($i = 1; $i <= 5; $i++){ $digit = rand(1,5); if($digit == 1){ $text = chr(rand(65,90)); } if($digit == 2){ $text = chr(rand(97,122)); } if($digit == 3){ $text = rand(0,9); } if($digit == 4){ $text = chr(rand(97,122)); } if($digit == 5){ $text = chr(rand(65,90)); } $textstyle = rand(1,5).".TTF"; $textsize = rand(20,50); $textangle = rand(-50,50); $x = $x + 80; imagettftext($image,$textsize,$textangle,$x,50,imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)),$textstyle,$text); } imagejpeg($image); ?> |
1 | /*//using this code $image= imagecreatefrompng("background.png");*/ |
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.
Program:
1 2 3 4 5 6 7 | <?php $img = null; $img = imagecreatefromjpeg("Desert.jpg"); $tmp_img = imagecreatetruecolor(250,250); imagecopyresized($tmp_img, $img, 0, 0, 0, 0,250,250,1024,768); imagejpg($tmp_img,"new.jpg"); ?> |
Output:
Return new.jpg image with new width 250px and new height 250px and the file size is 9.60kb which is very small than
Explanation:
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
PHP Image Compression
Resize image without changing width and height.
1 2 3 4 5 6 7 8 | <?php header('Content-type: image/jpeg'); $filename = 'Desert.jpg'; $image = imagecreatefromjpeg($filename); $dest='temp_image.jpg'; imagejpeg($image, null, 70); //its return image in the browser directly imagejpeg($image, $dest, 70); //its return new jpeg image in the project location. ?> |
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.