2013-07-16 20 views
6

Mam skrypt zmiany rozmiaru obrazu, który ma obraz 130x81 i dodaje go do obrazu 130x130, gdy funkcja imagecopyresample wybierze to dodaje czarne tło do pozostałej przestrzeni, nawet jeśli obraz podstawowy jest biały. Kod poniżej, naprawdę mógłbym docenić jakąś pomoc.php imagecopyresampled dodaje czarne tło

Obraz Staram się łączyć na 130x130 plików PHP stworzony jest: 130x81 image

$width = 130; 
$height = 130; 
$filename = 'process-add.jpg'; //130x81px jpg 
$this->_image = imagecreatefromjpeg($filename); 

$background = imagecreatetruecolor(130,130);//create the background 130x130 
$whiteBackground = imagecolorallocate($background, 255, 255, 255); 
imagefill($background,0,0,$whiteBackground); // fill the background with white 

imagecopyresampled($background, $this->_image,(130-$width)/2,(130-$height)/2, 0, 0, $width, $height, $width, $height); // copy the image to the background 

ImageJpeg ($background,null,100); //display 

Czytałem na wielu stanowiskach dodać:

imagealphablending($background, false); 

do kodu, który powinien naprawić to, ale to nie robi żadnej różnicy.

Z góry dziękuję!

Odpowiedz

10

Zostało to rozwiązane. Problem dotyczył szerokości i wysokości wywołania imagecopyresampled. Zobacz poniższy blok kodu:

<? 

ini_set('allow_url_fopen', true); 
$filename = 'http://img.yessy.com/1402152287-17201a.jpg'; // 130x81 
$image = imagecreatefromjpeg($filename); 
list($originalWidth, $originalHeight) = getimagesize($filename); 

// Size of image to create 
$width = 130; 
$height = 130; 

$background = imagecreatetruecolor($width, $height);//create the background 130x130 
$whiteBackground = imagecolorallocate($background, 255, 255, 255); 
imagefill($background,0,0,$whiteBackground); // fill the background with white 

imagecopyresampled($background, $image, 0, ($height - $originalHeight)/2, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight); // copy the image to the background 

header("Content-type: image/jpeg"); 
ImageJpeg ($background,null,100); //display 
?> 
Powiązane problemy