1 / 24

Graphics

Graphics. Graphics. Yep, PHP can deal with graphics Well, not all on it's own Use GD Library http://www.boutell.com/gd/ Create Images Manipulate Images We will touch the tip of the Iceberg…. Image Functions. getimagesize( 'filename' )

sybil
Download Presentation

Graphics

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. Graphics

  2. Graphics • Yep, PHP can deal with graphics • Well, not all on it's own • Use GD Library • http://www.boutell.com/gd/ • Create Images • Manipulate Images • We will touch the tip of the Iceberg…

  3. Image Functions • getimagesize('filename') • Returns FALSE if cannot open file or not valid image • Otherwise returns a 7 element array • Most useful to us are the first 4 elements: • Width • Height • Type (Number that corresponds to the type) • height="yyy" width="xxx" string for use in IMG tag

  4. Image Types 1 = GIF 2 = JPG 3 = PNG 4 = SWF 5 = PSD 6 = BMP 7 = TIFF (intel byte order) 8 = TIFF (motorola byte order) 9 = JPC 10 = JP2 11 = JPX 12 = JB2 13 = SWC 14 = IFF 15 = WBMP 16 = XBM

  5. getimagesize() • if ($imagedetails = getimagesize('Winter.jpg')) { • echo “<p>Width: {$imagedetails[0]}</p>"; • echo "<p>Height: {$imagedetails[1]}</p>"; • echo "<p>Type: {$imagedetails[2]}</p>"; • echo "<p>String: {$imagedetails[3]}</p>"; • } else { • echo "<p>Could not access file or file • not a valid image.</p>"; • }

  6. getimagesize() if ($imagedetails = getimagesize('Winter.jpg')) { • if ($imagedetails = getimagesize('Winter.jpg')) { • echo "<p>Width: {$imagedetails[0]}</p>"; • echo "<p>Height: {$imagedetails[1]}</p>"; • echo "<p>Type: {$imagedetails[2]}</p>"; • echo "<p>String: {$imagedetails[3]}</p>"; • } else { • echo "<p>Could not access file or file • not a valid image.</p>"; • } Attempt to get the details from the specified file. If successful $imagedetails will be an array containing the image information.

  7. getimagesize() • if ($imagedetails = getimagesize('Winter.jpg')) { • echo "Width: {$imagedetails[0]}<br />"; • echo "Height: {$imagedetails[1]}<br />"; • echo "Type: {$imagedetails[2]}<br />"; • echo "String: {$imagedetails[3]}<br />"; • } else { • echo “<p>Could not access file or file • not a valid image.</p>"; • } echo "<p>Width: {$imagedetails[0]}</p>"; echo "<p>Height: {$imagedetails[1]}</p>"; echo "<p>Type: {$imagedetails[2]}</p>"; echo "<p>String: {$imagedetails[3]}</p>"; On success, echo the information

  8. getimagesize() • if ($imagedetails = getimagesize('Winter.jpg')) { • echo “<p>Width: {$imagedetails[0]}</p>"; • echo “<p>Height: {$imagedetails[1]}</p>"; • echo “<p>Type: {$imagedetails[2]}</p>"; • echo “<p>String: {$imagedetails[3]}</p>"; • } else { • echo "Could not access file or file • not a valid image.<br />"; • } Otherwise, tell the user what was possibly wrong. • echo “<p>Could not access file or file • not a valid image.</p>";

  9. getimagesize() • if (list($width, $height, $type, $string) = • getimagesize('Winter.jpg')) { • echo “<p>Width: $width</p>"; • echo “<p>Height: $height</p>"; • echo “<p>Type: $type</p>"; • echo “<p>String: $string</p>"; • } else { • echo “<p>Could not access file or file • not a valid image.</p>"; • } This does the same thing using the list() function

  10. getimagesize() if (list($width, $height, $type, $string) = getimagesize('Winter.jpg')) { • if (list($width, $height, $type, $string) = • getimagesize('Winter.jpg')) { • echo "<p>Width: $width</p>"; • echo "<p>Height: $height</p>"; • echo "<p>Type: $type</p>"; • echo "<p>String: $string</p>"; • } else { • echo “<p>Could not access file or file • not a valid image.</p>"; • } The list() function takes data from an array and puts it into the specified variables

  11. Image Functions • imagecreatefrom*('filename') • e.g.imagecreatefromjpeg('filename') imagecreatefromgif('filename') imagecreatefrompng('filename') • Returns identifier representing image or empty string on failure

  12. Image Functions • imagecreatetruecolor(width, height) • Returns identifier representing a black image of size width xheight

  13. Image Functions • imagecopyresampled($destination, $source, dest_x, dest_y, src_x, src_y, dest_width, dest_height, src_width, src_height) • Used to copy a portion of one image to another • xand ypositions denote upper left hand corner • widthand heightare the distance from the specified xand y

  14. Image Functions • image*() • Creates image for output to browser or file • imagejpeg($source [,'filename',quality]) • imagegif($source [,'filename']) • imagepng($source [,'filename',quality,filters]) • $sourceis an image identifier • 'filename'required if writing to a file • Qualitydetermines amount of compression on a jpeg or png • jpg: 0 -100 (low-high quality), png: 0 (no compression) - 9

  15. Example • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo“<p>Could not access file or file not a valid image.</p>"; • }

  16. Example $in_image= 'Sunset.jpg'; • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } Set the filename of the image we want to manipulate

  17. Example if ($imgdetails= getimagesize($in_image)) { • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } If we successfully get the image details we can go to work.

  18. Example • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } switch ($imgdetails[2]) { case 2: // JPG File $img_src= imagecreatefromjpeg($in_image); break; } Use a switch statement to check the image type and load image accordingly. (In this case we know it's a jpeg)

  19. Example Set the destination image ready to accept the resampled input image. This is done by creating a true colour image half the size of the input image. • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } $img_dst= imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5);

  20. Example • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } Resample the source image into the blank destination. Set start coordinates to 0, 0 in both (so we can take whole image). Set required in and out dimensions (again full image) imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]);

  21. Example • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } Create our output jpeg file which will be saved in the same directory. imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg

  22. Example • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"<p>Could not access file or file not a valid image.</p>"; • } Destroy the open images imagedestroy($img_src); imagedestroy($img_dst);

  23. Example • $in_image = 'Sunset.jpg'; • if ($imgdetails = getimagesize($in_image)) { • switch ($imgdetails[2]) { • case 2: // JPG File • $img_src = imagecreatefromjpeg($in_image); • break; • } • $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); • imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); • imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg • imagedestroy($img_src); • imagedestroy($img_dst); • } else { • echo"Could not access file or file not a valid image.<br />"; • } Output an error message if the initial getimagesize call failed echo"<p>Could not access file or file not a valid image.</p>";

  24. HOE:Manipulating Graphics

More Related