问题是调整大小的图像是黑色背景,我看到这里有一些问题,但我无法做到!我尝试了一切,你能帮助我吗?谢谢这是我的代码:
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 :(得分:-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);