Comment dois-je remplir un fond blanc alors que redimensionnement de l'image
D'arrière-plan est noir. Comment changer la couleur blanche?
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
$width = $info[0];
$height = $info[1];
$x_ratio = $tn_w / $src_w;
$y_ratio = $tn_h / $src_h;
if (($x_ratio * $height) < $tn_w) {
$new_h = ceil($x_ratio * $height);
$new_w = $tn_w;
} else {
$new_w = ceil($y_ratio * $width);
$new_h = $tn_h;
}
$x_mid = $new_w / 2;
$y_mid = $new_h / 2;
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);
#if we need to add a watermark
if ($wmsource) {
#find out what type of image the watermark is
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
die('Invalid watermark type.');
}
#if we're adding a watermark, figure out the size of the watermark
#and then place the watermark image on the bottom right of the image
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
}
if (imagejpeg($final, $destination, $quality)) {
return true;
}
return false;
}
Noir & Blanc
OriginalL'auteur Blur | 2010-12-18
Vous devez vous connecter pour publier un commentaire.
Ici est l'ensemble de votre script (testé avec le portrait, le paysage et le carré jpg):
Il y a quelques erreurs de logique dans votre dernier imagecopyresampled(). Je pense que vous pouvez utiliser imagecopy() ici, parce que l'image des ressources sont à leur taille finale à ce point, de sorte que le fichier de réponse est plus proche de la solution, mais a besoin de quelques travaux (le positionnement semble être erronée, mais le fond est bon maintenant).
En fait j'ai utilisé le script à partir de goo.gl/62sXo et de modifier le calcul de la partie pour obtenir de conserver les proportions de l'image. je suis coincé
Merci beaucoup, j'ai vraiment besoin qu'une seule ligne de code. 😀
Fonctionne comme un charme.
OriginalL'auteur István Ujj-Mészáros
C'est de travailler pour moi. Bien qu'il semble logiquement il devrait remplir l'ensemble de l'image avec le $bgcolor, il remplit seulement les pièces qui sont "derrière" l'objet d'un nouvel échantillonnage de l'image.
OriginalL'auteur Lisa DeBruine