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

Dynapic

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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
Dynapic <? /* Documentation (im sorry for my english) *public properties* none *public methods* DynaPic(string pic, string format) -> class constructor ***parameters string pic -> full pic name, including the path (absolute or relative) string format -> the format of the input file, be sure that GDLib handle it! destroy() -> free the resources bool create() -> initialize the handling of the pic, thats not required after the constructor but could be required after some method calls like setPicName($pic), i.e. setPicName(string pic) -> change the handled pic, the class require a call to create() after this ***parameters string pic -> full pic name, including the path (absolute or relative) string getPicName() -> returns the name of the handled pic setThumbDimension(float pctg) -> initialize the dimensions of the default thumbnail ***parameters float pctg -> float number (0<pctg<=1) which specifies the resize factor of the default thumb, i.e. pctg=1 means that the thumb is like the original picture, pctg=0.5 means that the thumb is half of the original, and so on... getThumbDimension(float &w,float &h) -> return as referenced parameters the default dimension on the thumbnail setPicFormat(string format) -> set a new output format for the loaded pic, require a call to create() after this ***parameters string format -> the format of the input file, be sure that GDLib handle it! string getPicFormat() -> returns the format of the output thumb() -> output the default thumbnail full() -> output the original image view(float width, float height) -> output a thumbnail, the dimension is specified in the parameters of the methodý ***parameters float width -> width of the output pic float height -> height of the output pic */ class DynaPic { var $m_sPicName; //file name with path var $m_sFormat; //file format, check m_asAllowedFormat var $m_hPic; //image handle var $m_bHandled; //flag to check if the pic is well loaded var $m_nThumbX; //width of the thumbnail var $m_nThumbY; //height of the thumbnail //possible pic formats var $m_asAllowedFormat = array("GIF","PNG","JPEG"); //and related headers var $m_aHeaders = array("GIF"=>"Content-type: image/gif","PNG"=>"Content-type: image/png","JPEG"=>"Content-type: image/jpeg"); function DynaPic() { die("Constructor requires parameters: string pic_path, string "); } function DynaPic($pic,$format) { $this->m_sFormat = strtoupper($format); //check if valid format if(!in_array($this->m_sFormat,$this->m_asAllowedFormat)) { die("Invalid format: ".stripslashes(htmlentities($this->m_sFormat))); } //initialize $this->m_sPicName=$pic; //thumb default resize 50% $this->setThumbDimension(0.5); if(!$this->create()) die("Unable to create ".stripslashes(htmlentities($this->m_sPicName))." as ".stripslashes(htmlentities($this->m_sFormat))); } function destroy() { if($this->m_bHandled) { imagedestroy($this->m_hPic); $this->m_bHandled=false; } } function create() { $this->destroy(); eval("\$this->[email protected]".$this->m_sFormat."('".$this->m_sPicName."');"); //check if init succeded if(!$this->m_hPic) { $this->m_bHandled=false; return false; } else { $this->m_bHandled=true; return true; } } function setPicName($pic) { $this->m_sPicName=$pic; $this->m_bHandled=false; } function getPicName() { return $this->m_sPicName; } function setThumbDimension($pctg) { if($pctg<=0 || $pctg>1) die("Not a valid resize factor"); $size = GetImageSize($this->m_sPicName); $this->m_nThumbX=$size[0]*$pctg; $this->m_nThumbY=$size[1]*$pctg; } function getThumbDimension(&$w,&$h) { $w=$this->m_nThumbX; $h=$this->m_nThumbY; } function setPicFormat($format) { //check if valid format if(!in_array($format,$this->m_asAllowedFormat)) { die("Invalid format: ".stripslashes(htmlentities($this->m_sFormat))); } else { $this->m_sFormat=strtoupper($format); $this->m_bHandled=false; } } function getPicFormat() { return $this->m_sFormat; } //display functions function thumb() { if($this->m_bHandled) { $size = GetImageSize($this->m_sPicName); $picTmp=imagecreate($this->m_nThumbX,$this->m_nThumbY); imagecopyresized($picTmp, $this->m_hPic, 0, 0, 0, 0, $this->m_nThumbX, $this->m_nThumbY, $size[0], $size[1]); header($this->m_aHeaders[$this->m_sFormat]); switch($this->m_sFormat) { case 'JPEG': imagejpeg($picTmp); break; case 'GIF': imagegif($picTmp); break; case 'PNG': imagepng($picTmp); break; } imagedestroy($picTmp); } } function full() { if($this->m_bHandled) { header($this->m_aHeaders[$this->m_sFormat]); switch($this->m_sFormat) { case 'JPEG': imagejpeg($this->m_hPic); break; case 'GIF': imagegif($this->m_hPic); break; case 'PNG': imagepng($this->m_hPic); break; } } } function view($width, $height) { if($this->m_bHandled) { $size = GetImageSize($this->m_sPicName); $picTmp=imagecreate($width,$height); imagecopyresized($picTmp, $this->m_hPic, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); header($this->m_aHeaders[$this->m_sFormat]); switch($this->m_sFormat) { case 'JPEG': imagejpeg($picTmp); break; case 'GIF': imagegif($picTmp); break; case 'PNG': imagepng($picTmp); break; } imagedestroy($picTmp); } } } /* sample use, view folder content in thumbs <script1.php> $handle=opendir($dirname); while ($file = readdir($handle)) { if($file=='.'||$file=='..' || is_dir($dirname.$file)) continue; //i suppose that only gif images r in that directory echo "<img border=\"0\" src=\"thumb.php?pic=".urlencode($dirname.$file)."&zoom=25&ext=gif\"><br>\n"; } closedir($handle); <thumb.php> include("dynapic.class.inc.php"); $hThumb = new DynaPic(urldecode($HTTP_GET_VARS["pic"]),$HTTP_GET_VARS["ext"]); $hThumb->setThumbDimension($HTTP_GET_VARS["zoom"]/100); $hThumb->thumb(); $hThumb->destroy(); //*/ ?>