上傳圖檔後自動做縮圖

CodeCharge Studio 2.x, 3.x, 4.x, 5.x
回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

上傳圖檔後自動做縮圖

文章 yehlu »

Add AfterProcessFile Event
Custom Code
//製作縮圖
$filename = $prod->FileUpload5->Getvalue();
if($filename<>""){
$source = "../../upload/ec/prod_h/".$filename;
$t1 = "../../upload/ec/prod_hs/".$filename;
$t2 = "../../upload/ec/prod_hb/".$filename;
if (!file_exists($t1)){
ImageCopyResizedTrue($source,$t1,"120","120");
}
if (!file_exists($t2)){
ImageCopyResizedTrue($source,$t2,"265","265");
}
}
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

砍圖是要連縮圖一起砍

文章 yehlu »

Add BeforeDeleteFile Event

//砍圖片
$filename = $prod->FileUpload5->State[0];
if($filename<>""){
@unlink("../../upload/ec/prod_hs/".$filename);
@unlink("../../upload/ec/prod_hb/".$filename);
}
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

PHP GD 縮圖程式碼

文章 yehlu »

function ImageCopyResizedTrue($src,$dest,$maxWidth,$maxHeight,$quality=100) {

//檢查檔案是否存在
//if (file_exists($src) && isset($dest)) {
if (file_exists($src) && !file_exists($dest)) {
$destInfo = pathInfo($dest);
$srcSize = getImageSize($src); //圖檔大小
$srcRatio = $srcSize[0]/$srcSize[1]; // 計算寬/高
$destRatio = $maxWidth/$maxHeight;
if ($destRatio > $srcRatio) {
$destSize[1] = $maxHeight;
$destSize[0] = $maxHeight*$srcRatio;
}
else {
$destSize[0] = $maxWidth;
$destSize[1] = $maxWidth/$srcRatio;
}


//GIF 檔不支援輸出,因此將GIF轉成JPEG
if ($destInfo['extension'] == "gif") $dest = substr_replace($dest, 'jpg', -3);

//建立一個 True Color 的影像
$destImage = imageCreateTrueColor($destSize[0],$destSize[1]);

//根據副檔名讀取圖檔
switch ($srcSize[2]) {
case 1: $srcImage = imageCreateFromGif($src); break;
case 2: $srcImage = imageCreateFromJpeg($src); break;
case 3: $srcImage = imageCreateFromPng($src); break;
default: return false; break;
}

//取樣縮圖
ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],
$srcSize[0],$srcSize[1]);

//輸出圖檔
switch ($srcSize[2]) {
case 1: case 2: imageJpeg($destImage,$dest,$quality); break;
case 3: imagePng($destImage,$dest); break;
}
return true;
}
else {
return false;
}
}
回覆文章

回到「CodeCharge Studio」