x|x|x[c[]] * e.g. 100x, x200, 100x50, 100x200c50, 100x200c * @param string $expr * @param number $returnWidth * @param number $returnHeight * @param number $returnCrop * @param number $x0 * @param number $y0 * @param number $xs * @param number $ys */ function parseResizeExpr($expr, &$returnWidth, &$returnHeight,&$returnCrop,&$x0,&$y0,&$xs,&$ys){ $x0=0;$y0=0;$xs=0;$ys=0; // start specified ? if (preg_match ("/^\((\d+),(\d+),(\d+),(\d+)\)/",$expr,$matches)){ // extract x0,y0,xs,ys $x0 = $matches[1]; $y0 = $matches[2]; $xs = $matches[3]; $ys = $matches[4]; $idx = strpos($expr, ')'); $expr = substr($expr,$idx+1); } // crop specified ? (at end of resizeExpr ) $idx = strpos($expr, 'c'); if ($idx!==false) { $returnCrop = substr($expr,$idx+1); if ($returnCrop=="") $returnCrop=50; $expr = substr($expr,0,$idx); } $idx = strpos($expr, 'x'); // starts with x if ($idx === 0) { $returnWidth = 0; $returnHeight = substr($expr, 1); } // ends with x elseif ($idx === strlen($expr) - 1) { $returnWidth = substr($expr, 0, strlen($expr)-1); $returnHeight = 0; } // contains x elseif ($idx !== false) { $returnWidth = substr($expr, 0, $idx); $returnHeight = substr($expr, $idx +1); } else { trigger_error('Invalid image dimensions', E_USER_ERROR); } } function resizeAndOuput($resizeExpr, $filename) { $fullname =$filename; //echo $fullname; // if (strlen($fullname) < strlen(BASE)) { // it should never happen! // notfound(); // die(); // } //$cacheKey = md5($fullname . '|' . $resizeExpr . '|' . $_SERVER['QUERY_STRING']); $cacheKey = md5($fullname . '|' . $resizeExpr ); $cacheDir = BASE . '/picscache/' . $resizeExpr . '/'; // create cache dir if no exists $image = imagecreatefromjpeg($fullname); if (!$image){ // bad image: return 404 notfound(); // die(); } // Get image dimensions $width = imagesx ($image); $height = imagesy ($image); $crop=-1; // Get new dimensions & crop parseResizeExpr($resizeExpr, $new_width, $new_height,$crop,$x0,$y0,$xs,$ys); if ($xs && $ys) { if (($xs+$x0)<=$width && ($ys+$y0) <=$height) { $width = $xs; $height = $ys; // crop image $image_tmp = imagecreatetruecolor($width, $height); imagecopy($image_tmp, $image, 0, 0, $x0, $y0, $width, $height); $image = $image_tmp; } } // limits check if ($new_height>MAX_HEIGHT) $new_height=MAX_HEIGHT; if ($new_width>MAX_WIDTH) $new_width = MAX_WIDTH; // non ingrandiamo l'immagine if ($new_height>$height) $new_height = $height; if ($new_width>$width) $new_width = $width; $ratio = $width/$height; // compute the unspecified dimension if ($new_width < 1) { $new_width = $new_height * $ratio; } elseif ($new_height < 1) { $new_height = $new_width/$ratio; } $new_ratio = $new_width/$new_height; $widthRatio = $new_width/$width; $heightRatio = $new_height/$height; if ($crop==-1) { // keep original ratio if ($new_ratio != $ratio) { if ($widthRatio > $heightRatio) { $new_width = $new_height*$ratio; } else { $new_height = $new_width/$ratio; } } $src_x=0; $src_y=0; } else { // cropping needs 2 dimensions $rratio = $ratio/$new_ratio; if ($rratio>1) { // horizontal crop $centerx = ($width/2); $destwidth = $new_width/$heightRatio; $rangex = $centerx-$destwidth/2; // crop shift // normalize crop between -1 and +1 // 0-> - 1 SX // 50-> 0 Center // 100-> 1 DX $crop=($crop-50)/50; $src_x=$centerx-($destwidth/2)+($rangex*$crop); $src_y = 0; $width = $width/$rratio; } else { // vertical crop $centery = ($height/2); $destheight = $new_height/$widthRatio; $rangey = $centery-$destheight/2; // crop shift $crop=($crop-50)/50; $src_y=$centery-($destheight/2)+($rangey*$crop); $src_x = 0; $height = $height*$rratio; } } // Resample $image_p = imagecreatetruecolor($new_width, $new_height); /* imagealphablending($image_p, false); imagesavealpha($image_p, true); */ imagecopyresampled($image_p, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $width, $height); imagedestroy($image); // Cache thumb //imagejpeg($image_p, $cacheFile, 85); //chmod ($cacheFile,0777); // Output outputPic($image_p); imagedestroy($image_p); } function outputPic($file) { header('Content-type: image/png'); imagepng($file); exit(); } function &parsePathInfo() { $pathElements = array(); if (isset($_SERVER['PATH_INFO'])) { $pathElements =& explode('/', $_SERVER['PATH_INFO']); // Remove first empty item unset($pathElements[0]); } return $pathElements; } function notfound() { // header('HTTP/1.0 404 Not Found'); //exit(); } function getParameterValue ($params,$name,$default=null,$default1=null){ if ($params && array_key_exists ($name,$params)){ if ($default1==null) { $value = $params[$name]; } else { $value = $default1; } } else $value = $default; return $value; } // starting point $dimensions = getParameterValue ($_GET,'size'); $imgpath = getParameterValue ($_GET,'url'); if ((file_exists("../".$imgpath)) and ($imgpath!="")) { }else{ $imgpath="public/pnu.jpg"; } //$imgpath =str_replace(" ","%20",$imgpath); $imgpath= realpath ("../".$imgpath); resizeAndOuput($dimensions, $imgpath); ?>