Как сделать обрезку главной фотографии по короткой стороне opencart 2.3?

Добрый день! Есть аткая модификация для opencart 2.3 она при загрузке обрезает все фото в квадраты. Помогите переделать так что бы обрезалось по меньшей стороне только главная картинка товара (в категориях, в поиске... и т.д.)
/catalog/model/tool/image.php
<?php
class ModelToolImage extends Model {
	public function resize($filename, $width, $height, $type = "") {

        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;
        }
    }
}
  • Вопрос задан
  • 17 просмотров
Пригласить эксперта
Ответы на вопрос 1
@maxeee52
В модель:

function cropsize($filename, $width, $height) {

        if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != str_replace('\\', '/', DIR_IMAGE)) {
            return null;
        }

        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        $old_image = $filename;
        $new_image = 'cache/' . substr($filename, 0, strrpos($filename, '.')) . '-cr-' . $width . 'x' . $height . '.' . $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);
                }
            }

            $image = new Image(DIR_IMAGE . $old_image);
            $image->cropsize($width, $height);
            $image->save(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;
        }

    }


В system/library/image

public function cropsize($width = 0, $height = 0) {
	    
	    	if (!$this->info['width'] || !$this->info['height']) {
	    		return;
	    	}
        
	        //afmetingen bepalen
	        $photo_width = $this->info['width']; 
	        $photo_height = $this->info['height'];
	        
	        $new_width = $width;
	        $new_height = $height;
	        
	        //als foto te hoog is
	        if (($photo_width/$new_width) < ($photo_height/$new_height)) {
	        
	        	$from_y = ceil(($photo_height - ($new_height * $photo_width / $new_width))/2);
	        	$from_x = '0';
	        	$photo_y = ceil(($new_height * $photo_width / $new_width)); 
	        	$photo_x = $photo_width;
	        
	        }
	        
	        //als foto te breed is
	        if (($photo_height/$new_height) < ($photo_width/$new_width)) {

	        	$from_x = ceil(($photo_width - ($new_width * $photo_height / $new_height))/2);
	        	$from_y = '0';
	        	$photo_x = ceil(($new_width * $photo_height / $new_height)); 
	        	$photo_y = $photo_height;

        	}
	        
	        //als verhoudingen gelijk zijn	
	        if (($photo_width/$new_width) == ($photo_height/$new_height)) {
	        
	        	$from_x = ceil(($photo_width - ($new_width * $photo_height / $new_height))/2);
	        	$from_y = '0';
	        	$photo_x = ceil(($new_width * $photo_height / $new_height)); 
	        	$photo_y = $photo_height;
	        
	        }
	        
	        	        
	       	$image_old = $this->image;
	        $this->image = imagecreatetruecolor($width, $height);
			
			if (isset($this->info['mime']) && $this->info['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);
			}
			
			imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
		
		
	        imagecopyresampled($this->image, $image_old, 0, 0, $from_x, $from_y, $new_width, $new_height, $photo_x, $photo_y);
	        imagedestroy($image_old);
	           
	        $this->info['width']  = $width;
	        $this->info['height'] = $height;

	    
	    }
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы