watermark="./logo.jpg"; } // $imageInfo是上傳圖片後返回的資訊 // $type - 1代表產生商品或文章的縮圖,2代表產生頭像縮圖 public function makeThumb($imageInfo,$with=360,$height=360,$type=1){ if($imageInfo['type']==".jpg"){ $src_image=imagecreatefromjpeg($imageInfo['source_img_path']); }else if($imageInfo['type']==".png"){ $src_image=imagecreatefrompng($imageInfo['source_img_path']); }else if($imageInfo['type']==".gif"){ $src_image=imagecreatefromgif($imageInfo['source_img_path']); } $image=imagecreatetruecolor($with,$height); $white=imagecolorallocate($image,255,255,255); // 將背景設為透明 imagecolortransparent($image,$white); imagefill($image,0,0,$white); // 開始判斷最終產生的縮圖尺寸,避免變形 $finalWidth=$with; $finalHeight=$height; $x=$y=0; if($imageInfo['width']<$with && $imageInfo['height']<$height){ // 這種情況,不展開源圖,將源圖放置在縮圖正中間 $finalWidth=$imageInfo['width']; $finalHeight=$imageInfo['height']; $x=round(($width-$finalWidth)/2); $y=round(($height-$finalHeight)/2); }else if($imageInfo['width']>$imageInfo['height']){ // 確定縮放比例 $scale=round($finalWidth/$imageInfo['width'],2); $finalHeight=round($scale*$imageInfo['height']); $y=round(($height-$finalHeight)/2); }else if($imageInfo['width']<$imageInfo['height']){ $scale=round($finalHeight/$imageInfo['height'],2); $finalWidth=round($scale*$imageInfo['height']); $x=round(($width-$finalWidth)/2); }else if($imageInfo['width']==$imageInfo['height']){ if($with>$height){ $scale=round($finalHeight/$imageInfo['height'],2); $finalWidth=round($scale*$imageInfo['width']); $x=round(($with-$finalWidth)/2); }else if($with<$height){ $scale=round($finalWidth/$imageInfo['width'],2); $finalHeight=round($scale*$imageInfo['height']); $y=round(($height-$finalHeight)/2); } } imagecopyresampled($image,$src_image,$x,$y,0,0,$finalWidth,$finalHeight,$imageInfo['width'],$imageInfo['height']); // 增加浮水印 if($finalWidth>=200 && $finalHeight>=260){ $watermark=imagecreatefromjpeg($this->watermark); imagecopymerge($image,$watermark,$finalWidth-100,$finalHeight-130,0,0,100,130,80); imagedestroy($watermark); } !is_dir($this->thumbPath) && mkdir($this->thumbPath); // 儲存路徑+名字 $this->thumbPath=$this->thumbPath."/".time().mt_rand()."_{$finalWidth}_{$finalHeight}.png"; // 最後統一產生png格式,清晰度較高 header("content-type:image/png"); imagepng($image,$this->thumbPath); imagedestroy($image); imagedestroy($src_image); }}if($_FILES){ $a=new Thumb(); // 實際圖片資訊是上傳處理過的資訊,這裡寫成了固定的 $a->makeThumb(array('width'=>430,'height'=>430,'type'=>'.jpg','source_img_path'=>'./11.jpg'));}?>
11.jpg
浮水印圖
執行該指令碼之後,產生的縮圖沒問題,正常儲存
但是頁面卻顯示小邊框
求解,謝謝諸位
回複內容:
watermark="./logo.jpg"; } // $imageInfo是上傳圖片後返回的資訊 // $type - 1代表產生商品或文章的縮圖,2代表產生頭像縮圖 public function makeThumb($imageInfo,$with=360,$height=360,$type=1){ if($imageInfo['type']==".jpg"){ $src_image=imagecreatefromjpeg($imageInfo['source_img_path']); }else if($imageInfo['type']==".png"){ $src_image=imagecreatefrompng($imageInfo['source_img_path']); }else if($imageInfo['type']==".gif"){ $src_image=imagecreatefromgif($imageInfo['source_img_path']); } $image=imagecreatetruecolor($with,$height); $white=imagecolorallocate($image,255,255,255); // 將背景設為透明 imagecolortransparent($image,$white); imagefill($image,0,0,$white); // 開始判斷最終產生的縮圖尺寸,避免變形 $finalWidth=$with; $finalHeight=$height; $x=$y=0; if($imageInfo['width']<$with && $imageInfo['height']<$height){ // 這種情況,不展開源圖,將源圖放置在縮圖正中間 $finalWidth=$imageInfo['width']; $finalHeight=$imageInfo['height']; $x=round(($width-$finalWidth)/2); $y=round(($height-$finalHeight)/2); }else if($imageInfo['width']>$imageInfo['height']){ // 確定縮放比例 $scale=round($finalWidth/$imageInfo['width'],2); $finalHeight=round($scale*$imageInfo['height']); $y=round(($height-$finalHeight)/2); }else if($imageInfo['width']<$imageInfo['height']){ $scale=round($finalHeight/$imageInfo['height'],2); $finalWidth=round($scale*$imageInfo['height']); $x=round(($width-$finalWidth)/2); }else if($imageInfo['width']==$imageInfo['height']){ if($with>$height){ $scale=round($finalHeight/$imageInfo['height'],2); $finalWidth=round($scale*$imageInfo['width']); $x=round(($with-$finalWidth)/2); }else if($with<$height){ $scale=round($finalWidth/$imageInfo['width'],2); $finalHeight=round($scale*$imageInfo['height']); $y=round(($height-$finalHeight)/2); } } imagecopyresampled($image,$src_image,$x,$y,0,0,$finalWidth,$finalHeight,$imageInfo['width'],$imageInfo['height']); // 增加浮水印 if($finalWidth>=200 && $finalHeight>=260){ $watermark=imagecreatefromjpeg($this->watermark); imagecopymerge($image,$watermark,$finalWidth-100,$finalHeight-130,0,0,100,130,80); imagedestroy($watermark); } !is_dir($this->thumbPath) && mkdir($this->thumbPath); // 儲存路徑+名字 $this->thumbPath=$this->thumbPath."/".time().mt_rand()."_{$finalWidth}_{$finalHeight}.png"; // 最後統一產生png格式,清晰度較高 header("content-type:image/png"); imagepng($image,$this->thumbPath); imagedestroy($image); imagedestroy($src_image); }}if($_FILES){ $a=new Thumb(); // 實際圖片資訊是上傳處理過的資訊,這裡寫成了固定的 $a->makeThumb(array('width'=>430,'height'=>430,'type'=>'.jpg','source_img_path'=>'./11.jpg'));}?>
11.jpg
浮水印圖
執行該指令碼之後,產生的縮圖沒問題,正常儲存
但是頁面卻顯示小邊框
求解,謝謝諸位
header("content-type:image/png"); 把這一段刪除試試?
你只是產生縮圖並不直接把縮圖顯示給瀏覽器,卻配置了瀏覽器輸出為png格式,顯示個方塊,就是瀏覽器要顯示圖片,但是卻沒有正確的圖片顯示。