產生縮圖之後的頁面顯示問題

來源:互聯網
上載者:User
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格式,顯示個方塊,就是瀏覽器要顯示圖片,但是卻沒有正確的圖片顯示。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.