private static function img_resizer($src, $quality, $w, $h, $saveas) { /* v2.5 with auto crop */ $r = 1; $e = strtolower(substr($src, strrpos($src, ".") + 1, 3)); if ($e == "jpg" || $e == "jpeg") { $OldImage = imagecreatefromjpeg($src) or $r = 0; } elseif ($e == "gif") { $OldImage = ImageCreateFromGif($src) or $r = 0; } elseif ($e == "bmp") { $OldImage = ImageCreateFromwbmp($src) or $r = 0; } elseif ($e == "png") { $OldImage = ImageCreateFromPng($src) or $r = 0; } else { _o("No es una imagen válida! (" . $e . ") -- " . $src); $r = 0; } if ($r) { list($width, $height) = getimagesize($src); // check if ratios match $_ratio = array($width / $height, $w / $h); if ($_ratio[0] != $_ratio[1]) { // crop image // find the right scale to use $_scale = min((double) ($width / $w), (double) ($height / $h)); // coords to crop $cropX = (double) ($width - $_scale * $w); $cropY = (double) ($height - $_scale * $h); // cropped image size $cropW = (double) ($width - $cropX); $cropH = (double) ($height - $cropY); $crop = ImageCreateTrueColor($cropW, $cropH); // crop the middle part of the image to fit proportions ImageCopy($crop, $OldImage, 0, 0, (int) ($cropX / 2), (int) ($cropY / 2), $cropW, $cropH); } // do the thumbnail $NewThumb = ImageCreateTrueColor($w, $h); if (isset($crop)) { // been cropped ImageCopyResampled($NewThumb, $crop, 0, 0, 0, 0, $w, $h, $cropW, $cropH); ImageDestroy($crop); } else { // ratio match, regular resize ImageCopyResampled($NewThumb, $OldImage, 0, 0, 0, 0, $w, $h, $width, $height); } _ckdir($saveas); ImageJpeg($NewThumb, $saveas, $quality); ImageDestroy($NewThumb); ImageDestroy($OldImage); } return $r; }
if ($e == "jpg") { list($width, $height) = getimagesize($src); $_ratio = array($width / $height, $w / $h); if ($_ratio[0] != $_ratio[1]) { $_scale = min((double) ($width / $w), (double) ($height / $h)); $cropX = (double) ($width - $_scale * $w); $cropY = (double) ($height - $_scale * $h); $cropW = (double) ($width - $cropX); $cropH = (double) ($height - $cropY); $crop = ImageCreateTrueColor($cropW, $cropH); ImageCopy($crop, $OldImage, 0, 0, (int) ($cropX / 2), (int) ($cropY / 2), $cropW, $cropH); } $NewThumb = ImageCreateTrueColor($w, $h); if (isset($crop)) { ImageCopyResampled($NewThumb, $crop, 0, 0, 0, 0, $w, $h, $cropW, $cropH); ImageDestroy($crop); } else { ImageCopyResampled($NewThumb, $OldImage, 0, 0, 0, 0, $w, $h, $width, $height); } } _ckdir($saveas); ImageJpeg($NewThumb, $saveas, $quality); ImageDestroy($OldImage); } if ($e == "jpg" || $e == "jpeg") { header('Content-Type: image/jpeg'); ImageJpeg($NewThumb); ImageDestroy($NewThumb); } else { echo "No es una imagen válida! (" . $e . ") -- " . $src; }