Padding an image with a background color
From Code Trash
Consider that you want so display an image of height and width 350x200. If it is larger then you can resize it relative to height or width. If it is less than 350x255 then some might consider leaving the file as it is but for the others they want to pad the image with some color and to make it 350x200. This code does that.
If an image is 100x100 and you want to center it and to make the picture 350x200 then this code.
//$img contains the full physical path of the image file // like /home/site/public_html/images/filename.jpg // for example the actual image size in need is 685,365 after resizing. function padd($img) { //685,365 $imgdest = imagecreatetruecolor(685,365); $src = $img; $imiz = @getimagesize($src); $le = (int)abs((685/2) - ($imiz[0]/2)); $to = (int)abs((365/2) - ($imiz[1]/2)); switch($imiz['mime']) { case 'image/jpeg': $imgsrc = imagecreatefromjpeg($src); imagecopy($imgdest,$imgsrc,$le,$to,0,0,$imiz[0],$imiz[1]); imagejpeg($imgdest,$src,100); break; case 'image/png': $imgsrc = imagecreatefrompng($src); imagecopy($imgdest,$imgsrc,$le,$to,0,0,$imiz[0],$imiz[1]); imagejpeg($imgdest,$src,100); break; case 'image/gif': $imgsrc = imagecreatefromgif($src); imagecopy($imgdest,$imgsrc,$le,$to,0,0,$imiz[0],$imiz[1]); imagejpeg($imgdest,$src,100); break; } }