Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

商品画像アップロード時のリサイズ処理 #4898

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/config/eccube/packages/eccube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ parameters:
eccube_result_cache_lifetime: 3600 # doctrineのresult cacheのlifetime.
eccube_result_cache_lifetime_short: 10 # doctrineのresult cacheのlifetime. 商品一覧画面など長期間キャッシュできない箇所で使用する.
eccube_content_maintenance_file_path: '%env(ECCUBE_MAINTENANCE_FILE_PATH)%'
eccube_product_image_resize: false
eccube_product_image_jpg_quality: 75
eccube_product_image_png_quality: 6
65 changes: 60 additions & 5 deletions src/Eccube/Controller/Admin/Product/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function index(Request $request, $page_no = null, Paginator $paginator)
* - デフォルト値
* また, セッションに保存する際は mtb_page_maxと照合し, 一致した場合のみ保存する.
**/
$page_count = $this->session->get('eccube.admin.order.search.page_count',
$page_count = $this->session->get('eccube.admin.product.search.page_count',
$this->eccubeConfig->get('eccube_default_page_count'));

$page_count_param = (int) $request->get('page_count');
Expand All @@ -183,7 +183,7 @@ public function index(Request $request, $page_no = null, Paginator $paginator)
foreach ($pageMaxis as $pageMax) {
if ($page_count_param == $pageMax->getName()) {
$page_count = $pageMax->getName();
$this->session->set('eccube.admin.order.search.page_count', $page_count);
$this->session->set('eccube.admin.product.search.page_count', $page_count);
break;
}
}
Expand Down Expand Up @@ -330,9 +330,64 @@ public function addImage(Request $request)
throw new UnsupportedMediaTypeHttpException();
}

$filename = date('mdHis').uniqid('_').'.'.$extension;
$image->move($this->eccubeConfig['eccube_temp_image_dir'], $filename);
$files[] = $filename;
// リサイズ処理の可否('eccube_product_image_resize')
if ($this->eccubeConfig->get('eccube_product_image_resize')) {

// 加工前の画像の情報を取得
list($origin_w, $origin_h, $type) = getimagesize($image);

// 加工前のファイルをフォーマット別に読み出す
switch ($type) {
case IMAGETYPE_JPEG:
$origin_image = imagecreatefromjpeg($image);
break;

case IMAGETYPE_PNG:
$origin_image = imagecreatefrompng($image);
break;

case IMAGETYPE_GIF:
$origin_image = imagecreatefromgif($image);
break;

default:
throw new RuntimeException('対応していないファイル形式です。: ', $type);
}

// 新しく描画するキャンバスを作成
$canvas = imagecreatetruecolor($origin_w, $origin_h);
imagecopyresampled($canvas, $origin_image, 0,0,0,0, $origin_w, $origin_h, $origin_w, $origin_h);

$filename = date('mdHis').uniqid('_').'.'.$extension;
$files[] = $filename;

// 保存先を指定
$resize_path = $this->eccubeConfig['eccube_temp_image_dir'] . '/' . $filename;

// 画像形式ごとの処理
switch ($type) {
case IMAGETYPE_JPEG:
imagejpeg($canvas, $resize_path, $this->eccubeConfig->get('eccube_product_image_jpg_quality'));
break;

case IMAGETYPE_PNG:
imagepng($canvas, $resize_path, $this->eccubeConfig->get('eccube_product_image_png_quality'));
break;

case IMAGETYPE_GIF:
imagegif($canvas, $resize_path);
break;
}

imagedestroy($origin_image);
imagedestroy($canvas);

} else {

$filename = date('mdHis').uniqid('_').'.'.$extension;
$image->move($this->eccubeConfig['eccube_temp_image_dir'], $filename);
$files[] = $filename;
}
}
}
}
Expand Down