Download presentation
Presentation is loading. Please wait.
Published byStephen Brooks Modified over 8 years ago
1
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Graphics
2
2010/11 : [2]Building Web Applications using MySQL and PHP (W1)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
2010/11 : [3]Building Web Applications using MySQL and PHP (W1)Graphics 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
2010/11 : [4]Building Web Applications using MySQL and PHP (W1)Graphics 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
2010/11 : [5]Building Web Applications using MySQL and PHP (W1)Graphics getimagesize() if ($imagedetails = getimagesize('Winter.jpg')) { echo “ Width: {$imagedetails[0]} "; echo " Height: {$imagedetails[1]} "; echo " Type: {$imagedetails[2]} "; echo " String: {$imagedetails[3]} "; } else { echo " Could not access file or file not a valid image. "; }
6
2010/11 : [6]Building Web Applications using MySQL and PHP (W1)Graphics getimagesize() if ($imagedetails = getimagesize('Winter.jpg')) { echo " Width: {$imagedetails[0]} "; echo " Height: {$imagedetails[1]} "; echo " Type: {$imagedetails[2]} "; echo " String: {$imagedetails[3]} "; } else { echo " Could not access file or file not a valid image. "; } Attempt to get the details from the specified file. If successful $imagedetails will be an array containing the image information. if ($imagedetails = getimagesize('Winter.jpg')) {
7
2010/11 : [7]Building Web Applications using MySQL and PHP (W1)Graphics getimagesize() if ($imagedetails = getimagesize('Winter.jpg')) { echo "Width: {$imagedetails[0]} "; echo "Height: {$imagedetails[1]} "; echo "Type: {$imagedetails[2]} "; echo "String: {$imagedetails[3]} "; } else { echo “ Could not access file or file not a valid image. "; } On success, echo the information echo " Width: {$imagedetails[0]} "; echo " Height: {$imagedetails[1]} "; echo " Type: {$imagedetails[2]} "; echo " String: {$imagedetails[3]} ";
8
2010/11 : [8]Building Web Applications using MySQL and PHP (W1)Graphics if ($imagedetails = getimagesize('Winter.jpg')) { echo “ Width: {$imagedetails[0]} "; echo “ Height: {$imagedetails[1]} "; echo “ Type: {$imagedetails[2]} "; echo “ String: {$imagedetails[3]} "; } else { echo "Could not access file or file not a valid image. "; } getimagesize() Otherwise, tell the user what was possibly wrong. echo “ Could not access file or file not a valid image. ";
9
2010/11 : [9]Building Web Applications using MySQL and PHP (W1)Graphics if (list($width, $height, $type, $string) = getimagesize('Winter.jpg')) { echo “ Width: $width "; echo “ Height: $height "; echo “ Type: $type "; echo “ String: $string "; } else { echo “ Could not access file or file not a valid image. "; } getimagesize() This does the same thing using the list() function
10
2010/11 : [10]Building Web Applications using MySQL and PHP (W1)Graphics if (list($width, $height, $type, $string) = getimagesize('Winter.jpg')) { echo " Width: $width "; echo " Height: $height "; echo " Type: $type "; echo " String: $string "; } else { echo “ Could not access file or file not a valid image. "; } getimagesize() The list() function takes data from an array and puts it into the specified variables if (list($width, $height, $type, $string) = getimagesize('Winter.jpg')) {
11
2010/11 : [11]Building Web Applications using MySQL and PHP (W1)Graphics Image Functions imagecreatefrom*('filename') e.g. imagecreatefromjpeg('filename') imagecreatefromgif('filename') imagecreatefrompng('filename') Returns identifier representing image or empty string on failure
12
2010/11 : [12]Building Web Applications using MySQL and PHP (W1)Graphics Image Functions imagecreatetruecolor(width, height) Returns identifier representing a black image of size width x height
13
2010/11 : [13]Building Web Applications using MySQL and PHP (W1)Graphics 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 x and y positions denote upper left hand corner width and height are the distance from the specified x and y
14
2010/11 : [14]Building Web Applications using MySQL and PHP (W1)Graphics Image Functions image*() -Creates image for output to browser or file imagejpeg($source [,'filename',quality]) imagegif($source [,'filename']) imagepng($source [,'filename',quality,filters]) -$source is an image identifier -'filename' required if writing to a file -Quality determines amount of compression on a jpeg or png jpg: 0 -100 (low-high quality), png: 0 (no compression) - 9
15
2010/11 : [15]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; }
16
2010/11 : [16]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } $in_image = 'Sunset.jpg'; Set the filename of the image we want to manipulate
17
2010/11 : [17]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } if ($imgdetails = getimagesize($in_image)) { If we successfully get the image details we can go to work.
18
2010/11 : [18]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } 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
2010/11 : [19]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } $img_dst = imagecreatetruecolor($imgdetails[0]*0.5,$imgdetails[1]*0.5); 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.
20
2010/11 : [20]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $imgdetails[0]*0.5, $imgdetails[1]*0.5, $imgdetails[0], $imgdetails[1]); 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)
21
2010/11 : [21]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } imagejpeg($img_dst, 'output.jpg', 80); // Save result as output.jpg Create our output jpeg file which will be saved in the same directory.
22
2010/11 : [22]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } imagedestroy($img_src); imagedestroy($img_dst); Destroy the open images
23
2010/11 : [23]Building Web Applications using MySQL and PHP (W1)Graphics 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. "; } echo " Could not access file or file not a valid image. "; Output an error message if the initial getimagesize call failed
24
2010/11 : [24]Building Web Applications using MySQL and PHP (W1)Graphics HOE: Manipulating Graphics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.