2013-03-08 205 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,並且所有圖像都將根據需要調整大小。