1 / 22

Image Handling with PHP

Image Handling with PHP . Image Generation & Manipulation. PHP offers powerful set of functions for generating and manipulating images files in a variety of different image formats, including gif, png , jpg, wbmp , and xpm . Examples: Render regular geometric figures, modifying images

carrie
Download Presentation

Image Handling with PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Image Handling with PHP

  2. Image Generation & Manipulation • PHP offers powerful set of functions for generating and manipulating images files in a variety of different image formats, including gif, png, jpg, wbmp, and xpm. • Examples: • Render regular geometric figures, modifying images • manipulate text, font and color and even the pixels in the image. • ….creating images on the fly. • Even more convenient, PHP can output image streams directly to a browser.

  3. Image Generation & Manipulation  • PHP uses the GD library for the most of the image functionality that it offers. • GD library is used for generating two-dimensional graphics. • PHP API’s provides us with functions to: • Create, delete, resize and modify images. • Draw basic geometric figures • Manipulate text and fonts • Manipulate colors • Interlace and manipulate pixels • Handle PostScript files

  4. Why Process Images • Automatic Watermarks • Watermark images viewed on ‘free’ page • Unmarked image on ‘pay-for’ page • Resize images on the fly • Thumbnail / Gallery View • Custom images for mobile devices • Create charts and graphs on the fly with dynamic data

  5. Process Once or at Every Request • Blogger, Flickr, etc. all make thumbnails at the point of upload. • For high volume operations, disk space is always cheaper than processor time • Processing Requests on the fly necessary if you are customizing images every time they are loaded • Charts are updated after a set duration

  6. Base image functions • image imagecreate(width,height) • Creates and returns an image of specified height • color imagecolorallocate(image, R,G,B) • Creates a color for image with the given Red, Green, and Blue colors • booleanimagefill(image, x, y, color) • Flood fills at the given point with the given color • header("Content-type: image/png"); • Displays the MIME header for a PNG image • imagepng(image) • Displays the data for the PNG image

  7. Example: Green image <? $image = imagecreate(300,200); $colorGreen = imagecolorallocate($image, 0, 255, 0); imagefill($image, 0, 0, $colorGreen); header("Content-type: image/png"); imagepng($image); ?> http://onajejohnston.com/courses/php/labs/greenimg.php

  8. Creating Images • imagecreatefromjpeg($path_to_file) • Creates an image object using a preexisting image file • There are functions for png, gif, bmp & jpeg • imagecreatetruecolor($height, $width) • Creates new image object • imagecreatefromstring($binary_data)

  9. Image Properties • imagesx($image_resource) • Returns the width (in pixels) of an image object • imagesy($image_resource) • Returns the height of an image object Aspect Ratio = x/y or w/h • When resizing images, aspect ratio must be maintained

  10. Outputting Images • imagejpeg($image_resource) • Outputs a jpeg format image to the browser • Other functions for png, gif, bmp etc. • imagejpeg($image_resource, $path_to_file) • Saves image to the server

  11. Image Processing • imagecopyresampled(lotsofargs) • Used to resize an image • imagecopymerge(lotsofargs) • Used to place an image on top of another • imagefilter(lotsofargs) • Manipulates the content of an image

  12. Graphic drawing • imagecreate($x, $y); //create image • imagepng($image); //create pngimage imagejpeg($image); //create jpegimage imagegif($image); //create gif image : : • imagedestroy(); //close image stream

  13. Graphic drawing • imageline($image, $x1, $y1, $x2, $y2, $color); • Imagepolygon($image, arraypoints, $num_points, $color) • Imagerectangle($image, $x1, $y1, $x2, $y2, $radius) • Imageellipse($image, $cx, $cy, $w, $h, $color) • Imagearc($image, $cx, $cy, $x, $y, $ang1, $ang2)

  14. Graphic drawing • Imagefill($image, $x, $y, $color); • Imagefilledpolygon($image, arraypoints, $num_points, $color) • Imagefilledrectangle($image, $x1, $y1, $x2, $y2, $radius $color) • Imagefilledellipse($image, $cx, $cy, $w, $h, $color) • Imagefilledarc($image, $cx, $cy, $x, $y, $ang1, $ang2, $color)

  15. Graphic drawing • Imagesetstyle($image, $style); //style • Imagesetbrush($image, $brush); //brush image • Imagecolortransparent($brush, $color); • Imagecreatefrompng(urlor file)

  16. Graphic drawing • imagestring ($image, $font, $x, $y, $string, $color ); • imagestringup ($image, $font, $x, $y, $string, $color ); • imagechar($image, $font, $x, $y, $string, $color ); • imagecharup($image, $font, $x, $y, $string, $color );

  17. Image example <? $image = imagecreate(300,200); $colorWhite = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $colorWhite); imagefilledrectangle($image, 50,50, 100, 75, imagecolorallocate($image,255,0,0)); imageellipse($image, 150, 50, 100, 50, imagecolorallocate($image,0,0,255)); imagestring($image, 0, 10, 10, "Hello!", imagecolorallocate($image,0,0,0)); header("Content-type: image/png"); imagepng($image); ?>

  18. ImagePolygon and ImageLine <? $image = imagecreate(300,200); $colorWhite = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $colorWhite); $colorBlack = imagecolorallocate($image, 0, 0, 0); imageLine($image, 50, 0, 200, 150, $colorBlack); $pointsTriangle = array(50, 10, 10, 90, 90, 90); imagePolygon($image, $pointsTriangle, count($pointsTriangle)/2, $colorBlack); header("Content-type: image/png"); imagepng($image); ?> How could you display this image multiple times in a web page?

  19. Irregular Polygons • imagefilledpolygon( resource_name, points_array, • number_of_points, colour); • The array of points is made up of a sequence of x,y pairs. • PHP draws a line from one point to another to create the polygon. • Note the use of the header( ) function. This tells the browser what sort of content it is going to be getting. • It must be sent before the content is and not after. <?php $points = array( 20, // x1, top-left 20, // y1 230, // x2, top-right 40, // y2 220, // x3, bottom-right 230, // y3 20, // x4, bottom-left 200 // y4 ); $image = imagecreatetruecolor(250, 250); $green = imagecolorallocate($image, 0, 255, 0); imagefilledpolygon($image, $points, 4, $green ); header('Content-type: image/png'); imagepng($image); imagedestroy($image); ?>

  20. A Create Image From Example <?PHP //create image object $myimage = imagecreatefromjpeg('logo.jpg'); //output the appropriate header header('Content-type: image/jpeg'); //send the image object to output imagejpeg($myimage); //clean up imagedestroy($myimage); ?> • http://oj311.aisites.com/imageex1.php

  21. An Image Properties Example <?PHP $path = $_GET['file']; $path = str_replace('../', '', $path); $myimage = imagecreatefromjpeg($path); $h = imagesy($myimage); $w = imagesx($myimage); $ar = $w/$h; ?> <html> <head> <title>Finds Image's Size</title> </head> <body> <h1>Your Image is:</h1> <p>X: <?PHP echo $w; ?> pixels wide</p> <p>Y: <?PHP echo $h; ?> pixels long</p> <p>Aspect Ratio: <?PHP echo $ar; ?></p> </body> </html> • http://oj311.aisites.com/imagesize.php?file=logo.jpg

  22. A Graph Example X1, y1 0,0 http://oj311.aisites.com/graph1.php 100,100 http://oj311.aisites.com/graph1.phps X2, y2

More Related