2013-03-08 139 views
0

我终于轮到整理我的网站后几大隆起。我遇到图片大小调整的问题。Opencart选项 - 图像调整大小

的代码如下:这是控制图像大小:

'image' => $this->model_tool_image->resize($option_value['image'], 285, 85), 

这使得所有图片285 X 85

我的问题是,一个自定义的页面上我有6x1的选项,另外3倍等等。因此,我需要图像具有不同的尺寸,最好在重新加载后保持它们的比例(尽管图像在实际中更大 - 即855x255)。

我试过$ auto,max_width = 100%(取代285/85)等等,但没有效果。

我猜选项大小将来自样式表的控制,但对我的生活,4个小时的搜索后,我找不到要摆脱这种代码的方式,并为它工作,我想。

我想不出另一种方式来修改样式表以外的图像大小,并且到“auto”(size)的任何关系都是多余的!

它的建议之前,基金是处于绝对0。因此,我无法购买扩展/ MODS等等。我提前道歉

进一步加入:此代码:

'image' => $this->model_tool_image->resize($option_value['image'], $width == $auto, $height == $auto), 

带来的图像到原来的大小,但给出了一个错误:

“通知:未定义变量:宽度在C:\ xampp \ htdocs \ OC \ vqmod \ vqcache \ vq2-catalog_controller_product_product.php on line 373“

这是上面的代码行(带有$ auto)。

所以,我可以添加到我的图像控制器,或或其他东西,允许许多不同大小的图像的多个调整大小,给予一半大小的图像?

这里是我想要做的(以图像形式)

http://imageshack.us/photo/my-images/152/optionsimages.png/

2日编辑:

我一直是这样玩弄3天,并已找遍已知的资源。为了帮助缘故,这是代码为:系统/库/ image.php

/** 
* 
* @param width 
* @param height 
* @param default char [default, w, h] 
*     default = scale with white space, 
*     w = fill according to width, 
*     h = fill according to height 
* 
*/ 
public function resize($width = 0, $height = 0, $default = '') { 
    if (!$this->info['width'] || !$this->info['height']) { 
     return; 
    } 

    $xpos = 0; 
    $ypos = 0; 
    $scale = 1; 

    $scale_w = $width/$this->info['width']; 
    $scale_h = $height/$this->info['height']; 

    if ($default == 'w') { 
     $scale = $scale_w; 
    } elseif ($default == 'h'){ 
     $scale = $scale_h; 
    } else { 
     $scale = min($scale_w, $scale_h); 
    } 

    if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') { 
     return; 
    } 

    $new_width = (int)($this->info['width'] * $scale); 
    $new_height = (int)($this->info['height'] * $scale);    
    $xpos = (int)(($width - $new_width)/2); 
    $ypos = (int)(($height - $new_height)/2); 

    $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, $xpos, $ypos, 0, 0, $new_width,  $new_height, $this->info['width'], $this->info['height']); 
    imagedestroy($image_old); 

    $this->info['width'] = $width; 
    $this->info['height'] = $height; 
} 

出于某种原因,在@param提到的“默认”,不能在所有访问!

帮我#1,你是我唯一的希望

+0

不仅是system/library/image.php文件。你也应该改变目录/ model/tool/image.php文件。从模型调用resize方法。 – hkulekci 2013-03-10 11:52:25

+0

谢谢,我会寻找未来的更新! – BenDB 2013-03-11 19:52:17

回答

0

Opencart的使用缓存来存储不同尺寸的所有图像不使用样式表,如此在自定义页面使用该功能,也只是创造新的图片保持比例。

$new_image = $this->model_tool_image->resize($option_value['image'], 855, 255); 

看一看类ModelToolImage(前>>工具)和图片(库),以了解这些功能做。

更新:

看到您的更新,你不能这样做。

'image' => $this->model_tool_image->resize($option_value['image'], $width == $auto, $height == $auto),

你必须定义大小。给你一个小例子:

$sizes = array(
    '1' => array('w' => 255, 'h' => 85), 
    '2' => array('w' => 200, 'h' => 200), 
    '3' => array('w' => 350, 'h' => 150), 
) 

$images = array(); 

foreach ($sizes as $key => $size) 
{ 
    $images[$key] = $this->model_tool_image->resize($option_value['image'], $size['w'], $size['h']); 
} 

从这里你有填充的数组($图像)所有图像大小,你可以用它来填充模板(.tpl)

+0

好吧,据我了解,这将适合“product.php”,但它是在“$ option_value_data [] = array(”等等,其中的product.tpl链接是 - “ BenDB 2013-03-09 10:16:57

+0

编辑/添加:通过多个图像大小,我的意思是(例如)3x 285x85,6x 200x200 ,2x 350x150等等,这些都需要保留它们的比例,而不是显示调整图像大小的(调整大小的)限制框/边框的大小 – BenDB 2013-03-09 12:51:41

+0

感谢您的更新,因为似乎在products.php上只有1个输入行,我如何控制图像大小和正确应用?我试过多个额外的“工具”,但似乎没有工作 – BenDB 2013-03-09 18:13:40

0

我尝试添加该问题opencart主分支,但它没有被接受。这(https://gist.github.com/hkulekci/4503178)修补程序是根据宽度或高度比例的图像。

在此修补程序中,有管理设置,目录调整大小功能更新和所有图像大小调整功能更新。如果你不明白,你可以问任何事情。

编辑:

这个补丁有两部分。第一个是管理员,第二个是目录。你不需要做管理部分。您应该只更改目录部分。在目录部分,首先,你应该改变/upload/catalog/model/tool/image.php/upload/system/library/image.php文件如以下(https://gist.github.com/hkulekci/4503178#file-my4-patch-L14)(https://gist.github.com/hkulekci/4503178#file-my4-patch-L50)

// '/upload/catalog/model/tool/image.php' file changes 

// Find this line 
public function resize($filename, $width, $height) { 

// Change it this 
public function resize($filename, $width, $height, $type = "") { 


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

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


// Find this 
$image->resize($width, $height); 

// Change this 
$image->resize($width, $height, $type); 


// '/upload/system/library/image.php' file changes 

// Find this 
public function resize($width = 0, $height = 0) { 

// Change this 
public function resize($width = 0, $height = 0, $type = "") { 


// Find these lines 
$scale = min($scale_w, $scale_h); 
if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') { 

// CHange these lines 
$scale = 1; 
if ($type == "w"){ 
    $scale = $scale_w; 
}else if ($type == "h"){ 
    $scale = $scale_h; 
}else{ 
    $scale = min($scale_w, $scale_h); 
} 
if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') { 
    return; 
} 

实例应用:(https://gist.github.com/hkulekci/4503178#file-my1-patch-L347

// At the end : 
// Comment for resize function 
/** 
* 
* @param width 
* @param height 
* @param type char [default, w, h] 
* default = scale with white space, 
* w = fill according to width, 
* h = fill according to height 
* 
*/ 
$image = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_wishlist_width'), $this->config->get('config_image_wishlist_height'), "w"); 
+0

嗨!感谢您的回复。现在,我知道我在这里看到的是超出我的。你能告诉我这是哪里吗?它看起来像是复制和粘贴到不同的文件中?或者有更好的方法来使用它? Thankyou – BenDB 2013-03-09 17:41:23

+0

我改变我的意见,更清晰。 – hkulekci 2013-03-10 00:19:02

1

这已经被这个线程解决:

Remove the image resize ratio in OpenCart

我简单地将“max”元素设置为1000,并控制th e元素包含通过CSS的选项,即max-width 330px,并且所有图像都将根据需要调整大小。