【问题标题】:Opencart 2.3.0.2 black background image PNGOpencart 2.3.0.2黑色背景图片PNG
【发布时间】:2018-04-25 10:21:46
【问题描述】:

我在 Opencart 2.3.0.2 上调整大小的 PNG 图像有一些问题。我认为我的代码不知道如何使用 PNG。这是CATALOG/MODEL/TOOL/IMAGE.php的修改代码

<?php
class ModelToolImage extends Model {
    public function resize($filename, $width, $height, $type = "f") {

        if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) {
            return;
        }

        $info = pathinfo($filename);

        $extension = $info['extension'];

        $old_image = $filename;

        $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.' . $extension;

        if (!file_exists(DIR_IMAGE . $new_image) || (filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image))) {
            $path = '';           

            $directories = explode('/', dirname(str_replace('../', '', $new_image)));

            foreach ($directories as $directory) {
                $path = $path . '/' . $directory;

                if (!file_exists(DIR_IMAGE . $path)) {
                    @mkdir(DIR_IMAGE . $path, 0777);
                }
            }

            list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image);

            if ($width_orig != $width || $height_orig != $height) {

                $scaleW = $width_orig/$width;
                $scaleH = $height_orig/$height;

                $image = new Image(DIR_IMAGE . $old_image);

                if ($scaleH > $scaleW) {
                    $_height = $height * $scaleW;

                    $top_x = 0;
                    $top_y = ($height_orig - $_height) / 2;

                    $bottom_x = $width_orig;
                    $bottom_y = $top_y + $_height;

                    $image->crop($top_x, $top_y, $bottom_x, $bottom_y);
                } elseif ($scaleH < $scaleW) {
                    $_width = $width * $scaleH;

                    $top_x = ($width_orig - $_width) / 2;
                    $top_y = 0;

                    $bottom_x = $top_x + $_width;
                    $bottom_y = $height_orig;

                    $image->crop($top_x, $top_y, $bottom_x, $bottom_y);
                }

                $image->resize($width, $height, $type);
                $image->save(DIR_IMAGE . $new_image);
            } else {
                copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
            }
        }        

        if ($this->request->server['HTTPS']) {
            return $this->config->get('config_ssl') . 'image/' . $new_image;
        } else {
            return $this->config->get('config_url') . 'image/' . $new_image;
        }
    }
}

当我使用此代码时,我的 png 图像具有黑色背景。老实说,我无法理解什么问题。 我只是在学习 PHP,无法完全理解问题所在。 这是原始代码,其中PNG图像可以并且是透明的

    <?php
class ModelToolImage extends Model {
    public function resize($filename, $width, $height) {
        if (!is_file(DIR_IMAGE . $filename)) {
            if (is_file(DIR_IMAGE . 'no_image.jpg')) {
                $filename = 'no_image.jpg';
            } elseif (is_file(DIR_IMAGE . 'no_image.png')) {
                $filename = 'no_image.png';
            } else {
                return;
            }
        }

        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        $image_old = $filename;
        $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;

        if (!is_file(DIR_IMAGE . $image_new) || (filectime(DIR_IMAGE . $image_old) > filectime(DIR_IMAGE . $image_new))) {
            list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);

            if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) {
                return DIR_IMAGE . $image_old;
            }

            $path = '';

            $directories = explode('/', dirname($image_new));

            foreach ($directories as $directory) {
                $path = $path . '/' . $directory;

                if (!is_dir(DIR_IMAGE . $path)) {
                    @mkdir(DIR_IMAGE . $path, 0777);
                }
            }

            if ($width_orig != $width || $height_orig != $height) {
                $image = new Image(DIR_IMAGE . $image_old);
                $image->resize($width, $height);
                $image->save(DIR_IMAGE . $image_new);
            } else {
                copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
            }
        }

        $imagepath_parts = explode('/', $image_new);
        $new_image = implode('/', array_map('rawurlencode', $imagepath_parts));

        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
            return $this->config->get('config_ssl') . 'image/' . $new_image;
        } else {
            return $this->config->get('config_url') . 'image/' . $new_image;
        }
    }
}

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    只需将此代码从函数'resize'复制到system/library/image.php中的'crop'

        if ($this->mime == 'image/png') {
            imagealphablending($this->image, false);
            imagesavealpha($this->image, true);
            $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
            imagecolortransparent($this->image, $background);
        } else {
            $background = imagecolorallocate($this->image, 255, 255, 255);
        }
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 2018-03-15
      • 1970-01-01
      • 2018-06-18
      • 2015-04-20
      • 1970-01-01
      • 2014-01-04
      • 2023-03-20
      • 2017-11-10
      相关资源
      最近更新 更多