php - 使用 PHP 创建图像并向其添加 Logo

标签 php image dynamic generator

由于我们的工作量如此之大,我正在尝试做的是加快构建网站的一些时间。我们倾向于一遍又一遍地做同样的事情,对于这些 Night Drop 形式,我们在下面有一个小图像预览。单击它将打开 PDF,但我想知道是否有一种方法可以自动执行此操作,以便自动创建图像预览,只需获取 Logo 并重新调整大小并将其放在顶部,如下所示。

这可能吗?所以它会从左边的空白表格开始,然后从网站上获取 logo.png 文件并将其调整为正确的尺寸并将其放在顶部中央,就像第二张图片一样。

如果这是一个愚蠢的问题,我很抱歉,如果它可行的话就太棒了!

谢谢:-)

最佳答案

我成功了!这是任何感兴趣的人的代码。

<?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);

关于php - 使用 PHP 创建图像并向其添加 Logo ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824014/

相关文章:

php - 如何使用 MySQL 将值从一个微调器获取到另一个微调器?

php - 在 FullCalendar 4 中显示我的数据库中的事件

javascript - 动态调整 iframe 的高度

c# - Dapper - 如何使用动态对象

jquery 模糊不适用于动态文本框

php - 连续运行 PHP 脚本

php - 使用 PHP 打印按起始字母分组的 DB 中的数据?

html - 使用javascript的模态窗口

image - 距离图像到matlab中的物体

python - 当我在 Odoo 中上传图像时,如何在对话框窗口中按图像格式进行过滤?