= 2 ) // {name.}ext $extension = array_pop($parts); if ( $area ) array_push($parts, "a$area"); else if ( $diagonal ) array_push($parts, "d$diagonal"); else if ( $maximum ) array_push($parts, "m$maximum"); else { if ( $width ) array_push($parts, "w$width"); if ( $height ) array_push($parts, "h$height"); } if ( isset($extension) ) array_push($parts, $extension); return _implode('.', $parts); } define ('PIN_NAME', 0); define ('PIN_WIDTH', 1); define ('PIN_HEIGHT', 2); define ('PIN_MAXIMUM', 3); define ('PIN_DIAGONAL', 4); define ('PIN_AREA', 5); function ParseImageName($name) { $result = array(); if ( sizeof($parts = _explode('.', $name)) >= 3 ) // {name.}size.ext { $extension = array_pop($parts); $sizeparams = strtolower(array_pop($parts)); $result[PIN_NAME] = _implode('.', $parts) . '.' . $extension; $result[PIN_WIDTH] = ($i = strpos($sizeparams, 'w')) === FALSE ? 0 : intval(substr($sizeparams, $i + 1)); $result[PIN_HEIGHT] = ($i = strpos($sizeparams, 'h')) === FALSE ? 0 : intval(substr($sizeparams, $i + 1)); $result[PIN_MAXIMUM] = ($i = strpos($sizeparams, 'm')) === FALSE ? 0 : intval(substr($sizeparams, $i + 1)); $result[PIN_DIAGONAL] = ($i = strpos($sizeparams, 'd')) === FALSE ? 0 : intval(substr($sizeparams, $i + 1)); $result[PIN_AREA] = ($i = strpos($sizeparams, 'a')) === FALSE ? 0 : intval(substr($sizeparams, $i + 1)); } else { $result[PIN_NAME] = $name; $result[PIN_WIDTH] = 0; $result[PIN_HEIGHT] = 0; $result[PIN_MAXIMUM] = 0; $result[PIN_DIAGONAL] = 0; $result[PIN_AREA] = 0; } return $result; } // array indices for getimagesize() define ('IMG_WIDTH', 0); define ('IMG_HEIGHT', 1); define ('IMG_TYPE', 2); define ('IMG_SIZETEXT', 3); // imagetypes for getimagesize() : IMG_TYPE define ('IMG_TYPE_GIF', 1); define ('IMG_TYPE_JPG', 2); define ('IMG_TYPE_PNG', 3); define ('IMG_TYPE_SWF', 4); function ResizeImage($dst_name = '', $src_name, $width = 0, $height = 0, $maximum = 0, $diagonal = 0, $area = 0, $enlarge = FALSE) { $result = FALSE; if ( strlen($src_name) && ($width || $height || $maximum || $diagonal || $area) ) { $src_info = getimagesize($src_name); if ( $area ) { $width = $src_info[IMG_WIDTH] * sqrt($area / ($src_info[IMG_WIDTH] * $src_info[IMG_HEIGHT])); $height = 0; } else if ( $diagonal ) { $width = $src_info[IMG_WIDTH] * $diagonal / sqrt($src_info[IMG_WIDTH] * $src_info[IMG_WIDTH] + $src_info[IMG_HEIGHT] * $src_info[IMG_HEIGHT]); $height = 0; } else if ( $maximum ) { if ( $src_info[IMG_WIDTH] > $src_info[IMG_HEIGHT] ) { $width = $maximum; $height = 0; } else { $width = 0; $height = $maximum; } } if ( !$width ) $width = $src_info[IMG_WIDTH] * $height / $src_info[IMG_HEIGHT]; else if ( !$height ) $height = $src_info[IMG_HEIGHT] * $width / $src_info[IMG_WIDTH]; if ( !strlen($dst_name) ) $dst_name = $src_name; if ( $enlarge || ($width < $src_info[IMG_WIDTH] && $height < $src_info[IMG_HEIGHT])) { switch ( $src_info[IMG_TYPE] ) { case IMG_TYPE_GIF : $src = imagecreatefromgif ($src_name); break; case IMG_TYPE_JPG : $src = imagecreatefromjpeg($src_name); break; case IMG_TYPE_PNG : $src = imagecreatefrompng ($src_name); break; default: $src = NULL; break; } $dst = imagecreatetruecolor($width, $height); imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $src_info[IMG_WIDTH], $src_info[IMG_HEIGHT]); switch ( $src_info[IMG_TYPE] ) { case IMG_TYPE_GIF : imagegif($dst, $dst_name, QUALITY); break; case IMG_TYPE_JPG : imagejpeg($dst, $dst_name, QUALITY); break; case IMG_TYPE_PNG : imagepng($dst, $dst_name, QUALITY); break; default: break; } imagedestroy($src); imagedestroy($dst); $result = TRUE; } } return $result; } function SendImage($filename) { $result = FALSE; $info = getimagesize($filename); switch ( $info[IMG_TYPE] ) { case IMG_TYPE_GIF : $type = 'GIF'; break; case IMG_TYPE_JPG : $type = 'JPEG'; break; case IMG_TYPE_PNG : $type = 'PNG'; break; default: $type = ''; break; } if ( strlen($type) ) { if ( $handle = fopen($filename, 'rb') ) { $buffer = fread ($handle, filesize ($filename)); fclose($handle); header("Content-Type: image/{$type}"); // header('Expires: '.gmdate('D, d M Y H:i:s').' GMT'); // header('Pragma: no-cache'); echo $buffer; $result = TRUE; } } return $result; } //===== processing ========================================================== if ( isset($src) && strlen($src) ) { $filename = DOCUMENT_ROOT.$src; // filename is relative to root of domain if ( $width || $height || $maximum || $diagonal || $area ) { $filename = BuildImageName($filename, $width, $height, $maximum, $diagonal, $area); // combine basename and sizeparameter } if ( file_exists($filename) ) { if ( !SendImage($filename) ) // deliver image $error = "'$src' - not an image"; } else { $info = ParseImageName($filename); // extract basename and sizeparameter if ( file_exists($info[PIN_NAME]) ) { if ( !ResizeImage($filename, $info[PIN_NAME], $info[PIN_WIDTH], $info[PIN_HEIGHT], $info[PIN_MAXIMUM], $info[PIN_DIAGONAL], $info[PIN_AREA]) ) // create image by resizing { $filename = $info[PIN_NAME]; // if sizing fails, take source file for delivering $error = "'$src' - resize failed"; } if ( !SendImage($filename) ) // deliver image $error = "'$src' - not an image"; } else $error = "'$src' - file does not exist"; } } else $error = 'no name specified'; if ( strlen($error) ) { if ( strlen(ERRORLOG) /* && file_exists(ERRORLOG) */ ) { if ( $handle = fopen(ERRORLOG, 'a+') ) { fwrite($handle, sprintf("%s - ERROR: %s\r\n", date('d.m.y h:i:s'), $error)); fclose($handle); } } echo "ERROR: $error
"; } ?>