使用PHP创建图像并为其添加徽标

时间:2011-07-26 00:32:01

标签: php image dynamic generator

我想要做的是加快构建网站的时间,因为我们有如此大的工作量。我们倾向于一遍又一遍地做同样的事情,对于这些Night Drop表单,我们在下面有一个小图像预览。点击它会打开PDF,但我想知道是否有办法自动化这个,所以图像预览将自动创建,只需取出徽标并重新调整大小,并将其放在顶部,如下所示。 / p>

这可能吗?因此,它将从左侧的空白表单开始,然后从网站上获取logo.png文件并将其重新调整为正确的尺寸,并将其放在顶部中心,就像在第二张图像上一样。

对不起,如果这是一个愚蠢的问题,如果可以的话,那就太棒了!

谢谢: - )

2 个答案:

答案 0 :(得分:3)

我得到了它的工作!以下是感兴趣的人的代码。

<?php
function resize($img, $w, $h, $newfilename) {

 //Check if GD extension is loaded
 if (!extension_loaded('gd') && !extension_loaded('gd2')) {
  trigger_error("GD is not loaded", E_USER_WARNING);
  return false;
 }

 //Get Image size info
 $imgInfo = getimagesize($img);
 switch ($imgInfo[2]) {
  case 1: $im = imagecreatefromgif($img); break;
  case 2: $im = imagecreatefromjpeg($img);  break;
  case 3: $im = imagecreatefrompng($img); break;
  default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
 }

 //If image dimension is smaller, do not resize
 if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
  $nHeight = $imgInfo[1];
  $nWidth = $imgInfo[0];
 }else{
                //yeah, resize it, but keep it proportional
  if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
   $nWidth = $w;
   $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
  }else{
   $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
   $nHeight = $h;
  }
 }
 $nWidth = round($nWidth);
 $nHeight = round($nHeight);

 $newImg = imagecreatetruecolor($nWidth, $nHeight);

 /* Check if this image is PNG or GIF, then set if Transparent*/  
 if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
  imagealphablending($newImg, false);
  imagesavealpha($newImg,true);
  $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
 }
 imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

 //Generate the file, and rename it to $newfilename
 switch ($imgInfo[2]) {
  case 1: imagegif($newImg,$newfilename); break;
  case 2: imagejpeg($newImg,$newfilename);  break;
  case 3: imagepng($newImg,$newfilename); break;
  default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
 }

   return $newfilename;
}


$img = "images/logo.png"; // File image location
$newfilename = "images/dropoff_preview.png"; // New file name for thumb
$w = 45;
$h = 45;

$thumbnail = resize($img, $w, $h, $newfilename);


$image = @$HTTP_GET_VARS['image']; // Useful if using in an img tag to call images
$image = str_replace(array("/", ".."), "", $image); // Prevent abuse
$overlay = $thumbnail;

$dir = '';

// A default image for the demo...remove if you wish.
if ($image == NULL) {
    $image = 'images/dropoff_blank.jpg';
}

// Find if image exists
if (!file_exists($dir . $image)) {
    die("Image does not exist.");
}

// Set offset from bottom-right corner
$w_offset = 57;
$h_offset = 230;

$extension = strtolower(substr($image, strrpos($image, ".") + 1));

// Load image from file
switch ($extension)
{
    case 'jpg':
        $background = imagecreatefromjpeg($dir . $image);
        break;
    case 'jpeg':
        $background = imagecreatefromjpeg($dir . $image);
        break;
    case 'png':
        $background = imagecreatefrompng($dir . $image);
        break;
    case 'gif':
        $background = imagecreatefromgif($dir . $image);
        break;
    default:
        die("Image is of unsupported type.");
}

// Find base image size
$swidth = imagesx($background);
$sheight = imagesy($background);

// Turn on alpha blending
imagealphablending($background, true);

// Create overlay image
$overlay = imagecreatefrompng($dir . $overlay);

// Get the size of overlay
$owidth = imagesx($overlay);
$oheight = imagesy($overlay);

// Overlay watermark
imagecopy($background, $overlay, $swidth - $owidth - $w_offset, $sheight - $oheight - $h_offset, 0, 0, $owidth, $oheight);

// Output header and final image
header("Content-type: image/jpeg");
header("Content-Disposition: filename=" . $image);
imagejpeg($background);

// Destroy the images
imagedestroy($background);
imagedestroy($overlay);

答案 1 :(得分:2)

function doResizeAndWatermark () {

    $image = 'myImage.jpg';
    $watermarkImage = 'logo.png';
    $x = 10;
    $y = 10;
    $resizeWidth = '100';
    $resizeHeight = '200';
    $imagesize = getimagesize ( $image );
    $newImage = $image;
    if ( ! copy ( $image, $this->newImage ) )
        die ( 'Copy Image Failed' );

    $image = imagecreatefromjpeg ( $image );
    $newImage = imagecreatetruecolor ( $imagesize [ 0 ], $imagesize [ 1 ] );

    if ( ! imagecopyresampled ( $newImage, $image, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $imagesize [ 0 ], $imagesize [ 1 ] ) ) {
        die ( 'Resizing Image Faild' );
    }
    $tmprslt = getimagesize ( $watermarkImage );

    $watermarkImageWidth = $tmprslt [ 0 ];
    $watermarkImageHeight = $tmprslt [ 1 ];
    $watermarkImage = imagecreatefrompng ( $watermarkImage );

    if ( ! imagecopyresampled ( $image, $watermarkImage, $x, $y, 0, 0, $watermarkImageWidth, $watermarkImageWidth, $watermarkImageWidth, $watermarkImageHeight ) )
        die ( 'Watermark Copy Image Failed' );

    imagejpeg ( $newImage, $image, 85 );
}