Puoi provare questo.
Codice PHP:
<?php
function thumb($sourceImage,$thumbFile) {
$maxWidth = 300;
$maxHeight = 300;
$newWidth = 280;
$newHeight = 187;
$positionX = 10;
$positionY = 56;
//------- Width and Height of source image
list($imgWidth, $imgHeight) = getimagesize($sourceImage);
//------- Creates an image from the source image
$image = imagecreatefromjpeg($sourceImage);
//------- Creates the white background image
$ni = imagecreatetruecolor($maxWidth,$maxHeight);
$white = imagecolorallocate($ni, 255, 255, 255);
imagefill($ni, 0, 0, $white);
//--------- Creates a new empty image with new dimensions
$newImage = imagecreatetruecolor($newWidth, $newHeight);
//--------- Copies and resamples the source image into the new empty image
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
//--------- Copies the the new image inside the white background
imagecopy($ni, $newImage, $positionX, $positionY, 0, 0, $newWidth, $newHeight);
//--------- Creates the new image on hard disk using path and name that are in $thumbFile
imagejpeg($ni, $thumbFile, 50);
imagedestroy($ni);
} // function thumb($sourceImage,$thumbFile)
//------ Primo parametro : URL dell'immagine da copiare
//------ Secondo parametro : Il percorso e il nome del file finale sull'hard disk
thumb("example.jpg","th_example.jpg");
?>
example.jpg
example.jpg
th_example.jpg
th_example.jpg