Пример #1
0
/**
 * Изменяет изображение согласно переданных параметров
 * @param string $src Исходный файл
 * @param string $dest Результирующий файл
 * @param int $maxwidth Ширина, в которую ресайзить исходное изображение
 * @param int $maxheight Высота
 * @param bool $is_square Делать квадратное изображение?
 * @param bool $watermark Накладывать ватермарк?
 * @param string $rgb Цвет подложки
 * @param int $quality Качество изображения
 * @return boolean
 */
function img_resize($src, $dest, $maxwidth, $maxheight = 160, $is_square = false, $watermark = false, $rgb = 0xffffff, $quality = 95)
{
    if (!file_exists($src)) {
        return false;
    }
    $upload_dir = dirname($dest);
    if (!is_writable($upload_dir)) {
        @chmod($upload_dir, 0777);
    }
    $size = getimagesize($src);
    if ($size === false) {
        return false;
    }
    $new_width = $size[0];
    $new_height = $size[1];
    // Определяем исходный формат по MIME-информации, предоставленной
    // функцией getimagesize, и выбираем соответствующую формату
    // imagecreatefrom-функцию.
    $format = mb_strtolower(mb_substr($size['mime'], mb_strpos($size['mime'], '/') + 1));
    $icfunc = 'imagecreatefrom' . $format;
    $igfunc = 'image' . $format;
    if (!function_exists($icfunc)) {
        return false;
    }
    if (!function_exists($igfunc)) {
        return false;
    }
    if ($format == 'png') {
        $quality = 10 - ceil($quality / 10);
    }
    if ($format == 'gif') {
        $quality = NULL;
    }
    $isrc = $icfunc($src);
    if ($new_height <= $maxheight && $new_width <= $maxwidth) {
        if ($watermark) {
            if ($format == 'png') {
                imagealphablending($isrc, true);
                imagesavealpha($isrc, true);
            }
            img_watermark($isrc, $new_width, $new_height);
            $igfunc($isrc, $dest, $quality);
        } else {
            @copy($src, $dest);
        }
        return true;
    }
    if ($is_square) {
        $idest = imagecreatetruecolor($maxwidth, $maxwidth);
        if ($format == 'jpeg') {
            imagefill($idest, 0, 0, $rgb);
        } else {
            if ($format == 'png' || $format == 'gif') {
                $trans = imagecolorallocatealpha($idest, 255, 255, 255, 127);
                imagefill($idest, 0, 0, $trans);
                imagealphablending($idest, true);
                imagesavealpha($idest, true);
            }
        }
        // вырезаем квадратную серединку по x, если фото горизонтальное
        if ($new_width > $new_height) {
            imagecopyresampled($idest, $isrc, 0, 0, round((max($new_width, $new_height) - min($new_width, $new_height)) / 2), 0, $maxwidth, $maxwidth, min($new_width, $new_height), min($new_width, $new_height));
        }
        // вырезаем квадратную верхушку по y,
        if ($new_width < $new_height) {
            imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $maxwidth, $maxwidth, min($new_width, $new_height), min($new_width, $new_height));
        }
        // квадратная картинка масштабируется без вырезок
        if ($new_width == $new_height) {
            imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $maxwidth, $maxwidth, $new_width, $new_width);
        }
    } else {
        if ($new_width > $maxwidth) {
            $wscale = $maxwidth / $new_width;
            $new_width *= $wscale;
            $new_height *= $wscale;
        }
        if ($new_height > $maxheight) {
            $hscale = $maxheight / $new_height;
            $new_width *= $hscale;
            $new_height *= $hscale;
        }
        $idest = imagecreatetruecolor($new_width, $new_height);
        if ($format == 'jpeg') {
            imagefill($idest, 0, 0, $rgb);
        } else {
            if ($format == 'png' || $format == 'gif') {
                $trans = imagecolorallocatealpha($idest, 255, 255, 255, 127);
                imagefill($idest, 0, 0, $trans);
                imagealphablending($idest, true);
                imagesavealpha($idest, true);
            }
        }
        imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
    }
    if ($watermark) {
        img_watermark($idest, $new_width, $new_height);
    }
    if ($format == 'jpeg') {
        imageinterlace($idest, 1);
    }
    // вывод картинки и очистка памяти
    $igfunc($idest, $dest, $quality);
    imagedestroy($isrc);
    imagedestroy($idest);
    return true;
}
Пример #2
0
function img_resize($src, $dest, $maxwidth, $maxheight = 160, $is_square = false, $watermark = false, $rgb = 0xffffff, $quality = 95)
{
    if (!file_exists($src)) {
        return false;
    }
    $upload_dir = dirname($dest);
    if (!is_writable($upload_dir)) {
        @chmod($upload_dir, 0777);
    }
    $size = getimagesize($src);
    if ($size === false) {
        return false;
    }
    $new_width = $size[0];
    $new_height = $size[1];
    $formats = array(1 => 'gif', 2 => 'jpeg', 3 => 'png', 6 => 'wbmp', 15 => 'wbmp');
    $format = @$formats[$size[2]];
    $icfunc = "imagecreatefrom" . $format;
    if (!function_exists($icfunc)) {
        return false;
    }
    $isrc = $icfunc($src);
    if ($new_height <= $maxheight && $new_width <= $maxwidth) {
        if ($watermark) {
            img_watermark($isrc, $new_width, $new_height);
            imagejpeg($isrc, $dest, $quality);
        } else {
            @copy($src, $dest);
        }
        return true;
    }
    if ($is_square) {
        $idest = imagecreatetruecolor($maxwidth, $maxwidth);
        imagefill($idest, 0, 0, $rgb);
        // вырезаем квадратную серединку по x, если фото горизонтальное
        if ($new_width > $new_height) {
            imagecopyresampled($idest, $isrc, 0, 0, round((max($new_width, $new_height) - min($new_width, $new_height)) / 2), 0, $maxwidth, $maxwidth, min($new_width, $new_height), min($new_width, $new_height));
        }
        // вырезаем квадратную верхушку по y,
        if ($new_width < $new_height) {
            imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $maxwidth, $maxwidth, min($new_width, $new_height), min($new_width, $new_height));
        }
        // квадратная картинка масштабируется без вырезок
        if ($new_width == $new_height) {
            imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $maxwidth, $maxwidth, $new_width, $new_width);
        }
    } else {
        if ($new_width > $maxwidth) {
            $wscale = $maxwidth / $new_width;
            $new_width *= $wscale;
            $new_height *= $wscale;
        }
        if ($new_height > $maxheight) {
            $hscale = $maxheight / $new_height;
            $new_width *= $hscale;
            $new_height *= $hscale;
        }
        $idest = imagecreatetruecolor($new_width, $new_height);
        imagefill($idest, 0, 0, $rgb);
        imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
    }
    if ($watermark) {
        img_watermark($idest, $new_width, $new_height);
    }
    imageinterlace($idest, 1);
    // вывод картинки и очистка памяти
    imagejpeg($idest, $dest, $quality);
    imagedestroy($isrc);
    imagedestroy($idest);
    return true;
}
Пример #3
0
        header("Content-Disposition: attachment; filename=" . getSafeCode($record['file_name'], "utf-8"));
        $fp = fopen($the_file, 'rb');
        fseek($fp, $pos_start);
        while (!feof($fp)) {
            $buffer = stream_get_contents($fp, $block_size);
            $pos_current += $block_size;
            if ($pos_current >= $pos_end) {
                echo substr($buffer, 0, $block_size - ($pos_current - $pos_end));
                break;
            } else {
                echo $buffer;
            }
        }
        fclose($fp);
    } else {
        header("HTTP/1.1 200 OK");
        header("Content-type: " . $record['file_type']);
        header("Accept-Ranges: bytes");
        header("Accept-Length: " . $record['file_size']);
        header("Content-Disposition: attachment; filename=" . getSafeCode($record['file_name'], "utf-8"));
        if (strpos($record['file_type'], "image") === 0 && ($setting['watermark']['mode'] & 2) == 2 && $record['watermark'] == 1) {
            img_watermark($the_file, ROOT_PATH . "/" . $setting['watermark']['img'], dirname($the_file) . "/cache/" . basename($the_file), $setting['watermark']['position'], array('rate' => $setting['watermark']['img_rate'], 'alpha' => $setting['watermark']['alpha'], 'font' => ROOT_PATH . "/" . $setting['watermark']['txt_font'], 'fontsize' => $setting['watermark']['txt_fontsize'], 'fontcolor' => $setting['watermark']['txt_fontcolor'], 'bgcolor' => $setting['watermark']['txt_bgcolor']));
        } else {
            readfile($the_file);
        }
    }
} else {
    $db->close();
    header("HTTP/1.0 404 Not Found");
}
unset($db);