2016-03-30 33 views
0

İlk olarak, PHP konusunda uzman değilim. Yeniden boyutlandırmak ve görüntüleri kırpmak için bir işlev kullanıyorum. Şeffaf bir png yükleyene kadar mükemmel çalışıyor. :)PHP - Görüntüyü saydam png olarak nasıl kopyalayabilirim?

Png'yi siyah arka plan ile kaydeder. Stackoverflow'ta bazı cevaplar buldum ama kodlarımla birleştiremiyorum.

İşte benim fonksiyonudur:

//resize and crop image 
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 90){ 
    $imgsize = getimagesize($source_file); 
    $width = $imgsize[0]; 
    $height = $imgsize[1]; 
    $mime = $imgsize['mime']; 

    switch($mime){ 
     case 'image/gif': 
      $image_create = "imagecreatefromgif"; 
      $image = "imagegif"; 
      $format = "gif"; 
      break; 

     case 'image/png': 
      $image_create = "imagecreatefrompng"; 
      $image = "imagepng"; 
      $quality = 7; 
      $format = "png"; 
      break; 

     case 'image/jpeg': 
      $image_create = "imagecreatefromjpeg"; 
      $image = "imagejpeg"; 
      $format = "jpg"; 
      break; 

     default: 
      return false; 
      break; 
    } 

    $dst_img = imagecreatetruecolor($max_width, $max_height); 
    $src_img = $image_create($source_file); 

    $width_new = $height * $max_width/$max_height; 
    $height_new = $width * $max_height/$max_width; 
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa 
    if($width_new > $width){ 
     //cut point by height 
     $h_point = (($height - $height_new)/2); 
     //copy image 
     imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new); 
    }else{ 
     //cut point by width 
     $w_point = (($width - $width_new)/2); 
     imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height); 
    } 


    // you can ignore these 4 lines. I'm using it for change the name. 
    $nameforimage = rand('11111111', '9999999999'); 
    $nameforimage2 = rand('11111111', '9999999999'); 
    $newname  = $nameforimage."_".$nameforimage2; 
    $newdir   = $dst_dir."".$newname.".".$format; 

    $image($dst_img, $newdir, $quality); 

    if($dst_img)imagedestroy($dst_img); 
    if($src_img)imagedestroy($src_img); 

    return $newname.".".$format; 

} 

DÜZENLEME: Bir çözüm bulduk tamam.

Sadece şu satırları ekleyin:

imagealphablending($dst_img, false); 
imagesavealpha($dst_img, true); 
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); 
imagefilledrectangle($dst_img, 0, 0, $max_width, $max_height, $transparent); 

bu hat sonra:

$dst_img = imagecreatetruecolor($max_width, $max_height); 
+0

benim yanlış kodları güncelledik, imagesavealpha() ile yapılabilir. Lütfen tekrar bakın .. –

+3

Aşağıdaki soruda seçilen cevap alfa harmanlamanın nasıl korunacağını gösterecektir: http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using- phps-GDlib-imagecopyresample. –

+0

Tamam, teşekkürler. Ben buldum ve şimdi benim soruma cevap katacak. –

cevap

1

Sen alfa kanalı tasarrufu sağlamak zorundayız.

O ör .:

// As per the manual, alpha blending must be disabled 
imagealphablending($dst_img, false); 
imagesavealpha($dst_img, true); 
+0

Teşekkür ederim. İşe yarıyor :). Yani bunlar gerekli mi, değil mi? >> transparan = imagecolorallocatealpha ($ dst_img, 255, 255, 255, 127); imagefilledrectangle ($ dst_img, 0, 0, $ max_width, $ max_height, $ saydam); '' –

+0

@InterestingKnox bana gereksiz geliyor, şeffaf bir dikdörtgen çiziyorsunuz, ancak kaynağı kopyalayacaksınız Görüntü üzerinde yine de. – outlyer

+0

Ah tamam! Başka bir cevaptan örnek bir koddu. Çok teşekkürler @outlyer. –

İlgili konular