240 likes | 403 Views
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' )
E N D
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') • 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
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
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>"; • }
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.
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
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>";
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
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
Image Functions • imagecreatefrom*('filename') • e.g.imagecreatefromjpeg('filename') imagecreatefromgif('filename') imagecreatefrompng('filename') • Returns identifier representing image or empty string on failure
Image Functions • imagecreatetruecolor(width, height) • Returns identifier representing a black image of size width xheight
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
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
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>"; • }
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
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.
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)
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);
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]);
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
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);
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>";