1.0) $ratio = 1.0; $altezza = imagesy($img); //altezza $larghezza = imagesx($img); //larghezza if ($altezza > $larghezza){ // Immagine verticale $sx = floor($box_w); $sy = floor(($altezza*$box_w)/$larghezza); } else { //compute sizes $sy = floor($box_h); $sx = floor(($larghezza*$box_h)/$altezza); } //compute margins //Using these margins centers the image in the thumbnail. //If you always want the image to the top left, //set both of these to 0 $m_y = floor(($box_h - $sy) / 2); $m_x = floor(($box_w - $sx) / 2); //Copy the image data, and resample // //If you want a fast and ugly thumbnail, //replace imagecopyresampled with imagecopyresized if(!imagecopyresampled($new, $img, $m_x, $m_y, //dest x, y (margins) 0, 0, //src x, y (0,0 means top left) $sx, $sy,//dest w, h (resample to this size (computed above) imagesx($img), imagesy($img)) //src w, h (the full size of the original) ) { //copy failed imagedestroy($new); return null; } //copy successful return $new; } $i = imagecreatefromjpeg("gallery/files/" . $_GET["img"]); $thumb = thumbnail_box($i, 630, 500); imagedestroy($i); if(is_null($thumb)) { /* image creation or copying failed */ header('HTTP/1.1 500 Internal Server Error'); exit(); } header('Content-Type: image/jpeg'); imagejpeg($thumb); ?>