简体   繁体   English

调整图像大小的PHP prestashop

[英]resize image php prestashop

I tried to resize my images on upload with prestashop but its not working can you help ? 我试图通过prestashop调整上传图片的大小,但无法正常工作,您能帮忙吗?

 $images = count($_FILES['specialImg']['name']);
        $minwidth = 1500;
        $minheight = 1500;
        if ($images <= Configuration::get('JMARKETPLACE_MAX_IMAGES')) {
            for ($i=1; $i<=Configuration::get('JMARKETPLACE_MAX_IMAGES'); $i++) {
                if ($_FILES['specialImg']['name'][$i] != "") {
                    if ((($_FILES['specialImg']['type'][$i] == "image/pjpeg") || 
                        ($_FILES['specialImg']['type'][$i] == "image/jpeg") || 
                        ($_FILES['specialImg']['type'][$i] == "image/png")) && 
                        ($_FILES['specialImg']['size'][$i] < $this->return_bytes(ini_get('post_max_size')))) {

                        list($width, $height, $type, $attr) = getimagesize($_FILES['specialImg']['tmp_name'][$i]);    
//HERE IS MY RESIZE                        
 ImageManager::resize($_FILES['specialImg']['tmp_name'][$i], 200, 200);

if ($width == $minwidth  && $height == $minheight) {
                       $url_images[$i] = $_FILES['specialImg']['tmp_name'][$i];
                   }

HERE prestashop resize function , but i have an error cant make it work , file are uploaded but not resized ?! 在这里prestashop调整大小功能,但是我无法执行它的错误,文件已上传但没有调整大小? can someone tell me how to do that 有人可以告诉我该怎么做吗

public static function resize($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg',
                            $force_type = false, &$error = 0, &$tgt_width = null, &$tgt_height = null, $quality = 5,
                            &$src_width = null, &$src_height = null)
{
    if (PHP_VERSION_ID < 50300) {
        clearstatcache();
    } else {
        clearstatcache(true, $src_file);
    }

    if (!file_exists($src_file) || !filesize($src_file)) {
        return !($error = self::ERROR_FILE_NOT_EXIST);
    }

    list($tmp_width, $tmp_height, $type) = getimagesize($src_file);
    $rotate = 0;
    if (function_exists('exif_read_data') && function_exists('mb_strtolower')) {
        $exif = @exif_read_data($src_file);

        if ($exif && isset($exif['Orientation'])) {
            switch ($exif['Orientation']) {
                case 3:
                    $src_width = $tmp_width;
                    $src_height = $tmp_height;
                    $rotate = 180;
                    break;

                case 6:
                    $src_width = $tmp_height;
                    $src_height = $tmp_width;
                    $rotate = -90;
                    break;

                case 8:
                    $src_width = $tmp_height;
                    $src_height = $tmp_width;
                    $rotate = 90;
                    break;

                default:
                    $src_width = $tmp_width;
                    $src_height = $tmp_height;
            }
        } else {
            $src_width = $tmp_width;
            $src_height = $tmp_height;
        }
    } else {
        $src_width = $tmp_width;
        $src_height = $tmp_height;
    }

    // If PS_IMAGE_QUALITY is activated, the generated image will be a PNG with .jpg as a file extension.
    // This allow for higher quality and for transparency. JPG source files will also benefit from a higher quality
    // because JPG reencoding by GD, even with max quality setting, degrades the image.
    if (Configuration::get('PS_IMAGE_QUALITY') == 'png_all'
        || (Configuration::get('PS_IMAGE_QUALITY') == 'png' && $type == IMAGETYPE_PNG) && !$force_type) {
        $file_type = 'png';
    }

    if (!$src_width) {
        return !($error = self::ERROR_FILE_WIDTH);
    }
    if (!$dst_width) {
        $dst_width = $src_width;
    }
    if (!$dst_height) {
        $dst_height = $src_height;
    }

    $width_diff = $dst_width / $src_width;
    $height_diff = $dst_height / $src_height;

    $ps_image_generation_method = Configuration::get('PS_IMAGE_GENERATION_METHOD');
    if ($width_diff > 1 && $height_diff > 1) {
        $next_width = $src_width;
        $next_height = $src_height;
    } else {
        if ($ps_image_generation_method == 2 || (!$ps_image_generation_method && $width_diff > $height_diff)) {
            $next_height = $dst_height;
            $next_width = round(($src_width * $next_height) / $src_height);
            $dst_width = (int)(!$ps_image_generation_method ? $dst_width : $next_width);
        } else {
            $next_width = $dst_width;
            $next_height = round($src_height * $dst_width / $src_width);
            $dst_height = (int)(!$ps_image_generation_method ? $dst_height : $next_height);
        }
    }

    if (!ImageManager::checkImageMemoryLimit($src_file)) {
        return !($error = self::ERROR_MEMORY_LIMIT);
    }

    $tgt_width  = $dst_width;
    $tgt_height = $dst_height;

    $dest_image = imagecreatetruecolor($dst_width, $dst_height);

    // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background.
    if ($file_type == 'png' && $type == IMAGETYPE_PNG) {
        imagealphablending($dest_image, false);
        imagesavealpha($dest_image, true);
        $transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127);
        imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $transparent);
    } else {
        $white = imagecolorallocate($dest_image, 255, 255, 255);
        imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $white);
    }

    $src_image = ImageManager::create($type, $src_file);
    if ($rotate) {
        $src_image = imagerotate($src_image, $rotate, 0);
    }

    if ($dst_width >= $src_width && $dst_height >= $src_height) {
        imagecopyresized($dest_image, $src_image, (int)(($dst_width - $next_width) / 2), (int)(($dst_height - $next_height) / 2), 0, 0, $next_width, $next_height, $src_width, $src_height);
    } else {
        ImageManager::imagecopyresampled($dest_image, $src_image, (int)(($dst_width - $next_width) / 2), (int)(($dst_height - $next_height) / 2), 0, 0, $next_width, $next_height, $src_width, $src_height, $quality);
    }
    $write_file = ImageManager::write($file_type, $dest_image, $dst_file);
    @imagedestroy($src_image);
    return $write_file;
}

嗨,看看这个功能,它在我的脚本中很完美,并允许透明图像: 调整图像大小php形式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM