Exemple #1
0
/**
 * Proportionally resize an image file
 *
 * @param string $format
 * @param string $fullpath
 * @param int $MAX_W (optional)
 * @param int $MAX_H (optional)
 *
 * @throws \Exception
 */
function resize_down($format, $fullpath, $MAX_W = 1024, $MAX_H = 1024)
{
    if ($format == 'jpg') {
        $format = 'jpeg';
    }
    // fix stupid mistake
    if (!($format == 'jpeg' || $format == 'gif' || $format == 'png')) {
        throw new \Exception('Invalid image format');
    }
    /* Try to avoid problems with memory limit */
    fudge_factor($format, $fullpath);
    /* Proceed with resizing */
    $func = 'imagecreatefrom' . $format;
    $src = $func($fullpath);
    if (!$src) {
        throw new \Exception('Invalid image format');
    }
    list($orig_w, $orig_h) = getimagesize($fullpath);
    $ratio = $orig_w * 1.0 / $orig_h;
    $w_oversized = $orig_w > $MAX_W;
    $h_oversized = $orig_h > $MAX_H;
    if ($w_oversized || $h_oversized) {
        $new_w = round(min($MAX_W, $ratio * $MAX_H));
        $new_h = round(min($MAX_H, $MAX_W / $ratio));
    } else {
        return;
        // Do nothing, image is small enough already
    }
    $dst = imagecreatetruecolor($new_w, $new_h);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_w, $new_h, $orig_w, $orig_h);
    imagedestroy($src);
    $func = 'image' . $format;
    $func($dst, $fullpath);
    imagedestroy($dst);
}
Exemple #2
0
/**
 * Proportionally resize an image file
 *
 * @param string $format
 * @param string $fullpath
 * @param int $MAX_W (optional)
 * @param int $MAX_H (optional)
 *
 * @throws \Exception
 */
function resize_down($format, $fullpath, $MAX_W = 1024, $MAX_H = 1024)
{
    if ('jpg' == $format) {
        $format = 'jpeg';
        // fix stupid mistake
    }
    if (!('jpeg' == $format || 'gif' == $format || 'png' == $format)) {
        throw new \Exception('Invalid image format');
    }
    /* Try to avoid problems with memory limit */
    fudge_factor($format, $fullpath);
    /* Proceed with resizing */
    $func = 'imagecreatefrom' . $format;
    $src = $func($fullpath);
    // try again, but ignore warnings for jpeg only
    if (!$src && 0 === strcmp('jpeg', $format)) {
        ini_set('gd.jpeg_ignore_warning', 1);
        $src = '@' . $func($fullpath);
    }
    // try again, but replace with placeholder image
    if (!$src) {
        $src = imagecreatetruecolor(150, 100);
        $bkgd_color = imagecolorallocate($src, 255, 255, 255);
        $font_color = imagecolorallocate($src, 0, 0, 0);
        imagefilledrectangle($src, 0, 0, 150, 100, $bkgd_color);
        imagestring($src, 3, 5, 5, 'Error loading image', $font_color);
    }
    list($orig_w, $orig_h) = getimagesize($fullpath);
    $ratio = $orig_w * 1.0 / $orig_h;
    $w_oversized = $orig_w > $MAX_W;
    $h_oversized = $orig_h > $MAX_H;
    if ($w_oversized || $h_oversized) {
        $new_w = round(min($MAX_W, $ratio * $MAX_H));
        $new_h = round(min($MAX_H, $MAX_W / $ratio));
    } else {
        return;
        // Do nothing, image is small enough already
    }
    $dst = imagecreatetruecolor($new_w, $new_h);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_w, $new_h, $orig_w, $orig_h);
    imagedestroy($src);
    $func = 'image' . $format;
    $func($dst, $fullpath);
    imagedestroy($dst);
}