Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > Graphics Code Examples

Thumbnail Script

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
Thumbnail Script <?php /* Configuration: set $thumb_size to be a default maximum width/height if not passed via get set $no_image to be an image to be used when GIF files are passed to the script (The GD library no longer supports gif images) set $site_config['path_thumbnail'] to be a write-permissable folder on your server relative to the DOCUMENT_ROOT for storing images so they cache on the server. set $quality to be a value 0-100 for the resulting thumbnail jpeg quality ______________________________________________________________________ Usage: <img src="thumb.php?file=FILE&size=SIZE"> Where: FILE = the file to retrieve SIZE = the maximum size of the thumbnail in pixels ______________________________________________________________________ */ //script configuration $thumb_size = 128; //all thumbnails are this maximum width or height if not specified via get $no_image = '/preview/images/no_image.png'; // used if no image could be found, or a gif image is specified $site_config['path_thumbnail']='/images/thumbnails/'; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOT $quality = 70; //define size of image (maximum width or height)- if specified via get. if ($_GET["size"]<>0) { $thumb_size=$_GET["size"]; } //determine filesystem - *nix or windows - and set document_root. if the file is outside of the web server root path, the document_root is ignored and the absolute path of the image is used instead. if (substr_count(strtolower($_ENV['OS']), "windows")>0) { //w32 filesystem $site_config['document_root'] = str_replace(str_replace("/","\\\\",$_SERVER["SCRIPT_NAME"]),"",$_SERVER["PATH_TRANSLATED"]); if (file_exists(str_replace("/","\\\\",$_GET['file']))) { $filename=str_replace("/","\\\\",$_GET['file']); } else { $filename=$site_config['document_root']."\\\\".str_replace("/","\\\\",$_GET['file']); } } else { //linux filesystem $site_config['document_root'] = $_SERVER['DOCUMENT_ROOT']; if (file_exists($_GET["file"])) { $filename=$_GET["file"]; } else { $filename=$site_config['document_root'].'/'.$_GET["file"]; } } $no_image=$site_config['document_root'].$no_image; if (file_exists($site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg')) { Header('Content-type: image/jpeg'); Header("Expires: Mon, 26 Jul 2030 05:00:00 GMT"); Header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size).'.jpeg')); echo (join('', file( $site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg' ))); exit; //no need to create thumbnail - it already exists in the cache } //determine php and gd versions $ver=intval(str_replace(".","",phpversion())); if ($ver>=430) { $gd_version=gd_info(); } $filename=str_replace("\'","'",$filename); //define the right function for the right image types $image_type_arr=getimagesize($filename); $image_type=$image_type_arr[2]; switch ($image_type) { case 2: // JPG $image = ImageCreateFromJPEG ($filename); break; case 3: // PNG $image = ImageCreateFromPNG ($filename); break; case 1: // GIF - replace file with a 'no image' png default: $image = ImageCreateFromPNG ($no_image); break; } //define size of original image $image_width = imagesx($image); $image_height = imagesy($image); //define size of the thumbnail $thumb_width = $thumb_size; //$image_width/$resize_factor; $factor = $image_width/$thumb_size; $thumb_height = $image_height / $factor; //($image_width.$width_factor); //$resize_factor; if ($thumb_height>$thumb_size) { $thumb_height = $thumb_size; //$image_width/$resize_factor; $factor = $image_height/$thumb_size; $thumb_width = $image_width / $factor; //($image_width.$width_factor); //$resize_factor; } //define size if sizex and sizey given if ($_GET["sizex"]<>0) { $thumb_width=$_GET["sizex"]; $thumb_height=$_GET["sizey"]; } $memsize=filesize($filename); //create the thumbnail if ($image_width<2000) //no point in resampling images larger than 2000 pixels wide - too much server processing overhead - a resize is more economical { if (substr_count(strtolower($gd_version['GD Version']), "2.")>0) { //GD 2.0 $thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height); imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height); } else { //GD 1.0 $thumbnail = imagecreate($thumb_width, $thumb_height); imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height); } } else { if (substr_count(strtolower($gd_version['GD Version']), "2.")>0) { //GD 2.0 $thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height); imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height); } else { //GD 1.0 $thumbnail = imagecreate($thumb_width, $thumb_height); imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height); } } //create thumbnail in the cache because it doesn't exist yet. Header('Content-type: image/jpeg'); Header("Expires: Mon, 26 Jul 2030 05:00:00 GMT"); Header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size).'.jpeg')); imagejpeg($thumbnail,$site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg',$quality); echo (join('', file( $site_config['document_root'].$site_config['path_thumbnail'].md5($filename.$thumb_size).'.jpeg'))); //clear memory imagedestroy ($image); imagedestroy ($thumbnail); ?>