2017-06-22 16 views
-2

問題は、サイズ変更された画像は黒い背景です、私はここにいくつかの問題があることを見たが、私はそれを作ることができない!私はすべてを試みた、あなたは私を助けることができますか?おかげでここに私のコードです:画像のサイズ変更php黒の背景

header ("Content-type: image/png"); 

// Traitement de l'image source 
$source = imagecreatefrompng(_PS_IMG_DIR_.'test/toasty.png'); 

$largeur_source = imagesx($source); 
$hauteur_source = imagesy($source); 

imagealphablending($source, true); 
imagesavealpha($source, true); 

$nWidth = 400; 
$nHeight = 400; 

$newImg = imagecreatetruecolor($nWidth, $nHeight); 
imagealphablending($newImg, true); 
imagesavealpha($newImg,true); 
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); 
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent); 
imagecopyresampled($newImg, $source, 0, 0, 0, 0, $nWidth, $nHeight, 
    $srcWidth, $srcHeight); 
$resize = _PS_IMG_DIR_.'test/resize.png'; 
imagepng($newImg, $resize); 
$source = $newImg; 

// Traitement de l'image destination 
$destination = imagecreatefrompng(_PS_IMG_DIR_.'test/toaster.png'); 
$largeur_destination = imagesx($destination); 
$hauteur_destination = imagesy($destination); 

// Calcul des coordonnées pour placer l'image source dans l'image de destination 
$destination_x = ($largeur_destination - $largeur_source)/2; 
$destination_y = ($hauteur_destination - $hauteur_source)/2; 

// On place l'image source dans l'image de destination 
//imagecopymerge($destination, $source, $destination_x, $destination_y, 0, 0, $largeur_source, $hauteur_source, 100); 
imagecopy($destination, $source, $destination_x, $destination_y, 0, 0, $largeur_source, $hauteur_source); 
$trost = _PS_IMG_DIR_.'test/trost.png'; 
// On affiche l'image de destination 
imagepng($destination ,$trost); 

imagedestroy($source); 
imagedestroy($destination); 
+0

このコードは現在何をしていますか?ログにはどのようなエラーがありますか? – mkaatman

答えて

-1

このコード私は画像のアップロード後にサムネイルを作成するために使用されます。私はそれがあなたのための仕事だと思う。

function resize($src, $dest, $desired_width='150') { 

    /* read the source image */ 

    $image_mime_types = array(
     'image/jpeg' => 'jpeg', 
     'image/png' => 'png', 
     'image/bmp' => 'bmp', 
    ); 
    $image_type = image_type_to_mime_type(exif_imagetype($src)); 
    $image_type1 = $image_mime_types[$image_type]; 
    if($image_type1 == 'png'){ 
     $source_image = imagecreatefrompng($src); 
    } 
    else if($image_type1 == 'jpeg'){ 
     $source_image = imagecreatefromjpeg($src); 
    } 
    else if($image_type1 == 'bmp'){ 
     $source_image = imagecreatefromwbmp($src); 
    } 
    else{ 
     $source_image = imagecreatefromjpeg($src); 
    } 

    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

    /* find the "desired height" of this thumbnail, relative to the desired width */ 
    $desired_height = floor($height * ($desired_width/$width)); 

    /* create a new, "virtual" image */ 
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height); 

    /* copy source image at a resized size */ 
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); 

    /* create the physical thumbnail image to its destination */ 
    return imagejpeg($virtual_image, $dest); 
} 
$src='D:/server/xampp/htdocs/project/image/test.png'; // Your image path. 
$dest='D:/server/xampp/htdocs/project/image/resize/test_thumb.png'; // Your destination image path with an image. 
$image_width=200; // Wanted resized image width only. 
resize($src,$dest,$image_width); 
関連する問題