2015-06-20 83 views
0

我試圖在圖片上插入圖片。我畫有方形空圖像內它放在下一個圖像必須被存儲:圖片GD - 將圖片插入方塊(PHP)

private function __generate_background() 
{ 
    $img = imagecreatetruecolor($this->settings['__image_size_data']['wrapper']['width'], $this->settings['__image_size_data']['wrapper']['height']); 
    $bg = imagecolorallocate($img, 0, 0, 0); 

    # draw background 
    imagefilledrectangle($img, 0, 0, 120, 20, $bg); 

    # set border line width and color 
    imagesetthickness($img, 1); 
    $img_color_white = imagecolorallocate($img, 255, 255, 255); 

    # draw rectangle 
    imagerectangle(
     $img, 
     $this->__wrapper_padding['top'], # px from the left corner [start] 
     $this->settings['__image_size_data']['image']['height'], # px from the top corner [start and end] 
     ($this->settings['__image_size_data']['image']['width'] + $this->__wrapper_padding['left']), 
     $this->__wrapper_padding['top'], 
     $img_color_white 
    ); 

    # save image 
    imagejpeg($img, $this->settings['__temp_image'], 100); 
} 

,這是我此步驟後:

img1

下一個步驟是在正方形內插入上傳的圖像(帶有白色邊框的圖像)。一切看起來都很好,但白色的邊框消失了 - 圖像應該是白色邊框的包裝......我希望你明白我的意思。這是代碼:

private function __insert_image_to_wrapper() 
{ 
    # loaded image from uploaded one - $this->image_inside 
    # load image wrapper (black content with square inside) 
    $this->temp_data['image_wrapper'] = imagecreatefromjpeg($this->settings['__temp_image']); 

    list($width, $height) = getimagesize($this->settings['__image']); 
    list($newWidth, $newHeight) = getimagesize($this->settings['__temp_image']); 

    $out = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($out, $this->temp_data['image_wrapper'], 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    imagecopyresampled($out, $this->image_inside, $this->__wrapper_padding['left'], $this->__wrapper_padding['top'], 0, 0, $newWidth, $newHeight, $newWidth, $newHeight); 
    imagejpeg($out, $this->settings['__temp_image'], 100); 
} 

,這就是我得到..

img2

正如你所看到的圖像是在邊界..問題出在哪裏?

回答

0

您可以通過來繪製您的上傳圖片您的邊框。你可以嘗試在矩形之前繪製圖像。所以像

private function __generate_background() { 
     // init, background, ... 

     $this->__insert_image_to_wrapper(); 

     # draw rectangle 
     imagerectangle(
      $img, 
      $this->__wrapper_padding['top'], # px from the left corner [start] 
      $this->settings['__image_size_data']['image']['height'], # px from the top corner [start and end] 
      ($this->settings['__image_size_data']['image']['width'] + $this->__wrapper_padding['left']), 
      $this->__wrapper_padding['top'], 
      $img_color_white 
     ); 

     # save image 
     imagejpeg($img, $this->settings['__temp_image'], 100); 
    } 
+0

我可以理解你的邏輯,但有問題。我想我不能在創建矩形或無效之前使用'__insert_image_to_wrapper()'方法...它給我錯誤'imagecreatefromjpeg(uploads/temp/4bMdgw1fQUUN2ue.jpg):無法打開流:沒有這樣的文件或目錄' –