Location: PHPKode > projects > Image Maker > imagemaker/imagemaker_create.php
<?php

/**
 * This code is part of the ImageMaker software (www.gerd-tentler.de/tools/imagemaker), copyright by
 * Gerd Tentler. Obtain permission before selling this code or hosting it on a commercial website or
 * redistributing it over the Internet or in any other medium. In all cases copyright must remain intact.
 *
 * ARGUMENTS
 *
 * $type     = image type (PNG, GIF, JPG)
 * $width    = image width (pixels)
 * $height   = image height (pixels)
 * $color    = image background color (RGB hexcode, e.g. "FFA080")
 * $file     = optional: path + filename (save image)
 * $object[] = type | sizes | colors | alignments | margins | rotation | opacity | text OR file path (type "image")
 *
 *     type       = "ellipse" / "poly:" + edges (e.g. "poly:5") / "image" / "text:" + font (e.g. "text:arial")
 *     sizes      = font size / width ; height (; line size)
 *     colors     = line color (RGB hexcode) ; fill color (RGB hexcode)
 *     alignments = horizontal ("left" / "center" / "right") ; vertical ("top" / "center" / "bottom")
 *     margins    = left ; right ; top ; bottom
 *     rotation   = angle (needs PHP >= 4.3.0)
 *     opacity    = optional (0 - 100; needs PHP >= 4.0.1)
 *     text       = optional; type "image": file path
 */

//========================================================================================================
// Configuration
//========================================================================================================
  $defType = 'PNG';
  $defWidth = 468;
  $defHeight = 60;
  $fontDir = 'fonts';
  $antialiasing = false;
  $alphaColor = 'FFFFFE';

//========================================================================================================
// GPC variables
//========================================================================================================
  if(!isset($type)) $type = $_REQUEST['type'];
  if(!isset($width)) $width = $_REQUEST['width'];
  if(!isset($height)) $height = $_REQUEST['height'];
  if(!isset($color)) $color = $_REQUEST['color'];
  if(!isset($file)) $file = $_REQUEST['file'];
  if(!isset($object)) $object = $_REQUEST['object'];

//========================================================================================================
// Functions
//========================================================================================================
  function alloc_color($id, $color, $alpha = 0) {
    if($color != '') {
      $color = ereg_replace('[^0-9a-fA-F]', '', $color);
      $rgb = array(hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)));

      if($alpha && function_exists('imageColorAllocateAlpha'))
        $color = imageColorAllocateAlpha($id, $rgb[0], $rgb[1], $rgb[2], $alpha);
      else $color = imageColorAllocate($id, $rgb[0], $rgb[1], $rgb[2]);
    }
    return $color;
  }

  function view_image($type, $id, $path = '') {
    switch(strtoupper($type)) {
      case 'PNG': if($path) imagePNG($id, $path);
                  else imagePNG($id);
                  break;
      case 'GIF': if(function_exists('imageTrueColorToPalette')) imageTrueColorToPalette($id, false, 256);
                  if($path) imageGIF($id, $path);
                  else imageGIF($id);
                  break;
      case 'JPG': if($path) imageJPEG($id, $path);
                  else imageJPEG($id);
                  break;
    }
  }

  function load_image($type, $path) {
    switch(strtoupper($type)) {
      case 'PNG': $id = @imageCreateFromPNG($path); break;
      case 'GIF': $id = @imageCreateFromGIF($path); break;
      case 'JPG': $id = @imageCreateFromJPEG($path); break;
    }
    return $id;
  }

  function copy_image($dstID, $srcID, $bCol = '', $opacity = 100) {
    global $width, $height;

    if($bCol) imageColorTransparent($srcID, $bCol);
    list($x, $y) = set_xy(imageSX($srcID), imageSY($srcID));

    if(function_exists('imageCopyMerge'))
      imageCopyMerge($dstID, $srcID, $x, $y, 0, 0, imageSX($srcID), imageSY($srcID), $opacity);
    else imageCopy($dstID, $srcID, $x, $y, 0, 0, imageSX($srcID), imageSY($srcID));
  }

  function rotate_image($oID, $bCol, $rotation) {
    global $oCnt;

    $message = '';
    $id = $oID;

    if(function_exists('imageRotate')) {
      if($id = imageRotate($oID, -$rotation, $bCol)) imageDestroy($oID);
      else $message = 'Could not rotate';
    }
    else $message = 'Rotation not supported by this PHP/GD version';

    return array($id, $message);
  }

  function create_image($width, $height, $bgColor = '', $lineColor = '', $fillColor = '', $alpha = 0) {
    if(function_exists('imageCreateTrueColor')) $id = imageCreateTrueColor($width, $height);
    else $id = imageCreate($width, $height);

    if($id) {
      if(function_exists('imageAlphaBlending')) imageAlphaBlending($id, true);
      if(function_exists('imageSaveAlpha')) imageSaveAlpha($id, true);

      if($bgColor != '') {
        $bgColor = alloc_color($id, $bgColor, $alpha);
        imageFill($id, round($width / 2), round($height / 2), $bgColor);
        if($alpha) imageColorTransparent($id, $bgColor);
      }
      if($lineColor != '') $lineColor = alloc_color($id, $lineColor);
      if($fillColor != '') $fillColor = alloc_color($id, $fillColor);
    }
    return array($id, $bgColor, $lineColor, $fillColor);
  }

  function set_xy($oWidth, $oHeight) {
    global $width, $height, $hAlign, $vAlign, $lMargin, $rMargin, $tMargin, $bMargin;

    if($hAlign == 'center') $x = round(($width - $oWidth) / 2) + $lMargin - $rMargin;
    else if($hAlign == 'right') $x = $width - $oWidth - $rMargin + $lMargin;
    else $x = $lMargin - $rMargin;

    if($vAlign == 'center') $y = round(($height - $oHeight) / 2) + $tMargin - $bMargin;
    else if($vAlign == 'bottom') $y = $height - $oHeight - $bMargin + $tMargin;
    else $y = $tMargin - $bMargin;

    return array($x, $y);
  }

  function prepare_text($text, $font, $size, $rotation, $imgWidth) {
    $text = str_replace("\r", '', stripslashes($text));
    $str = explode("\n", trim($text));
    $strw = array();

    if($arr = @imageTtfbBox($size, -$rotation, $font, '$8Qgpqjy_')) $fHeight = $arr[1] - $arr[7];
    else $fHeight = imageFontHeight(font);
    $tHeight = count($str) * $fHeight;

    for($i = $tWidth = 0; $i < count($str); $i++) {
      $str[$i] = trim($str[$i]);
      if($arr = @imageTtfbBox($size, -$rotation, $font, $str[$i])) $strw[$i] = $arr[2] - $arr[0];
      else $strw[$i] = imageFontWidth(font) * strlen($str[$i]);
      if($strw[$i] > $tWidth) $tWidth = $strw[$i];
    }
    return array($str, $strw, $fHeight, $tHeight + round($fHeight / 5), $tWidth + round($fHeight / 5));
  }

  function view_message($dstID, $msg) {
    global $width, $height, $hAlign, $vAlign;

    $font = 2;
    $color = 'FFFFFF';
    $bgColor = '6060FF';
    $margin = 20;
    $hAlign = $vAlign = 'center';

    $cnt = count($msg);
    $fHeight = imageFontHeight($font);
    $oHeight = $fHeight * $cnt + $margin;
    for($i = $maxLen = 0; $i < $cnt; $i++) if(strlen($msg[$i]) > $maxLen) $maxLen = strlen($msg[$i]);
    $oWidth = $maxLen * imageFontWidth($font) + $margin;

    list($id, $bgCol, $lineCol) = create_image($oWidth+1, $oHeight+1, $bgColor, $color);

    if($id) {
      imageFilledRectangle($id, 0, 0, $oWidth, $oHeight, $bgCol);
      $x = $y = round($margin / 2);

      for($i = 0; $i < $cnt; $i++) {
        imageString($id, $font, $x, $y, $msg[$i], $lineCol);
        $y += $fHeight;
      }
      copy_image($dstID, $id, '', 80);
      imageDestroy($id);
    }
  }

  function object_image($oID, $oWidth, $oHeight, $rotation, $file) {
    global $width, $height, $alphaColor;

    $message = '';
    $ext = substr($file, strrpos($file, '.') + 1);

    if($imgID = load_image($ext, $file)) {
      list($id, $bCol) = create_image($oWidth, $oHeight, $alphaColor);

      if($id) {
        imageCopyResized($id, $imgID, 0, 0, 0, 0, $oWidth, $oHeight, imageSX($imgID), imageSY($imgID));

        if($rotation) list($id, $message) = rotate_image($id, $bCol, $rotation);

        copy_image($oID, $id, $bCol, 100);
        imageDestroy($id);
      }
      else $message = 'Internal error';

      imageDestroy($imgID);
    }
    else $message = 'Could not load image "' . $file . '"';

    return $message;
  }

  function object_ellipse($oID, $oWidth, $oHeight, $rotation) {
    global $width, $height, $alphaColor, $lineColor, $fillColor, $lineSize;

    $message = '';
    list($id, $bCol, $lCol, $fCol) = create_image($oWidth+2, $oHeight+2, $alphaColor, $lineColor, $fillColor);

    if($id) {
      $oMidX = round($oWidth / 2);
      $oMidY = round($oHeight / 2);

      if($lineSize && function_exists('imageSetThickness')) imageSetThickness($id, $lineSize);
      if(function_exists('imageEllipse')) {
        if($fillColor != '') imageFilledEllipse($id, $oMidX, $oMidY, $oWidth, $oHeight, $fCol);
        if($lineColor != '') imageEllipse($id, $oMidX, $oMidY, $oWidth, $oHeight, $lCol);
      }
      else if($lineColor != '') {
        imageArc($id, $oMidX, $oMidY, $oWidth, $oHeight, 0, 360, $lCol);
        if($fillColor != '') imageFill($id, $oMidX, $oMidY, $fCol);
      }
      if($rotation) list($id, $message) = rotate_image($id, $bCol, $rotation);

      copy_image($oID, $id, $bCol, 100);
      imageDestroy($id);
    }
    else $message = 'Internal error';

    return $message;
  }

  function object_poly($oID, $oWidth, $oHeight, $rotation, $edges) {
    global $lineColor, $fillColor, $lineSize, $lineCol, $fillCol;

    $message = '';
    $oMidX = round($oWidth / 2);
    $oMidY = round($oHeight / 2);
    $x = $y = 0;

    switch($edges) {
      case 3: $points = array($x, $y - $oMidY,
                              $x + $oMidX, $y + $oMidY,
                              $x - $oMidX, $y + $oMidY);
              break;
      case 4: $points = array($x - $oMidX, $y - $oMidY,
                              $x + $oMidX, $y - $oMidY,
                              $x + $oMidX, $y + $oMidY,
                              $x - $oMidX, $y + $oMidY);
              break;
      case 5: $xlen = round($oWidth / 3);
              $points = array($x, $y - $oMidY,
                              $x + $oMidX, $y,
                              $x + $xlen, $y + $oMidY,
                              $x - $xlen, $y + $oMidY,
                              $x - $oMidX, $y);
              break;
      case 6: $ylen = round($oHeight / 3);
              $points = array($x, $y - $oMidY,
                              $x + $oMidX, $y - $ylen,
                              $x + $oMidX, $y + $ylen,
                              $x, $y + $oMidY,
                              $x - $oMidX, $y + $ylen,
                              $x - $oMidX, $y - $ylen);
              break;
      case 7: $xlen = round($oWidth / 3);
              $ylen = round($oHeight / 3);
              $points = array($x, $y - $oMidY,
                              $x + $oMidX, $y - $ylen,
                              $x + $oMidX, $y + $ylen,
                              $x + $xlen, $y + $oMidY,
                              $x - $xlen, $y + $oMidY,
                              $x - $oMidX, $y + $ylen,
                              $x - $oMidX, $y - $ylen);
              break;
      case 8: $xlen = round($oWidth / 3);
              $ylen = round($oHeight / 3);
              $points = array($x - $xlen, $y - $oMidY,
                              $x + $xlen, $y - $oMidY,
                              $x + $oMidX, $y - $ylen,
                              $x + $oMidX, $y + $ylen,
                              $x + $xlen, $y + $oMidY,
                              $x - $xlen, $y + $oMidY,
                              $x - $oMidX, $y + $ylen,
                              $x - $oMidX, $y - $ylen);
              break;
      default: $edges = 0; break;
    }

    if($edges) {
      $a = deg2rad($rotation);
      list($xoffset, $yoffset) = set_xy($oWidth, $oHeight);
      $xoffset += $oMidX;
      $yoffset += $oMidY;

      for($i = 0; $i < count($points); $i += 2) {
        if($rotation) {
          $x = $points[$i];
          $y = $points[$i+1];
          $points[$i] = round($x * cos($a) - $y * sin($a)) + $xoffset;
          $points[$i+1] = round($x * sin($a) + $y * cos($a)) + $yoffset;
        }
        else {
          $points[$i] += $xoffset;
          $points[$i+1] += $yoffset;
        }
      }
      if($lineSize && function_exists('imageSetThickness')) imageSetThickness($oID, $lineSize);
      if($fillColor != '') imageFilledPolygon($oID, $points, $edges, $fillCol);
      if($lineColor != '') imagePolygon($oID, $points, $edges, $lineCol);
    }
    else $message = 'Edges must be between 3 and 8';

    return $message;
  }

  function object_text($oID, $fName, $fSize, $rotation, $text) {
    global $width, $lineColor, $fillColor, $lineSize, $lineCol, $fillCol, $oBGCol, $fontDir, $antialiasing;

    $message = '';
    $font = $fontDir . '/' . $fName . '.ttf';
    list($str, $strw, $fHeight, $tHeight, $tWidth) = prepare_text($text, $font, $fSize, $rotation, $width);

    if(!$antialiasing && function_exists('imageTtfText')) {
      $lineCol = -$lineCol;
      $fillCol = -$fillCol;
      $bgCol = -$oBGCol;
    }
    else $bgCol = $oBGCol;

    for($i = 0; $i < count($str); $i++) {
      list($x, $y) = set_xy($strw[$i], $tHeight);
      $y += round($tHeight*0.8 / count($str)) + $fHeight * $i;

      if($lineColor != '') {
        for($j = 1; $j <= $lineSize; $j++) {
          for($k = 0; $k < $j; $k++) {
            if(!@imageTtfText($oID, $fSize, -$rotation, $x+$k, $y+$j, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x+$k, $y-$fHeight+$j, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x+$k, $y-$j, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x+$k, $y-$fHeight-$j, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x-$k, $y+$j, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x-$k, $y-$fHeight+$j, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x-$k, $y-$j, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x-$k, $y-$fHeight-$j, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x+$j, $y+$k, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x+$j, $y-$fHeight+$k, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x-$j, $y+$k, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x-$j, $y-$fHeight+$k, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x+$j, $y-$k, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x+$j, $y-$fHeight-$k, $str[$i], $lineCol);
            }
            if(!@imageTtfText($oID, $fSize, -$rotation, $x-$j, $y-$k, $lineCol, $font, $str[$i])) {
              imageString($oID, 3, $x-$j, $y-$fHeight-$k, $str[$i], $lineCol);
            }
          }
        }
      }

      if($fillColor != '') {
        if(!@imageTtfText($oID, $fSize, -$rotation, $x, $y, $fillCol, $font, $str[$i])) {
          imageString($oID, 3, $x, $y-$fHeight, $str[$i], $fillCol);
        }
      }
      else {
        if(!@imageTtfText($oID, $fSize, -$rotation, $x, $y, $bgCol, $font, $str[$i])) {
          imageString($oID, 3, $x, $y-$fHeight, $str[$i], $bgCol);
        }
      }
      $y += $fHeight;
    }
    return $message;
  }

//========================================================================================================
// Main
//========================================================================================================
  if(!$type) $type = $defType;
  if(!$width) $width = $defWidth;
  if(!$height) $height = $defHeight;

  if($color) list($mainID, $mainBGCol) = create_image($width, $height, $color);
  else list($mainID, $mainBGCol) = create_image($width, $height, $alphaColor, '', '', 127);

  if($mainID) {
    $message = array();

    for($oCnt = 0; $oCnt < count($object); $oCnt++) {
      $oID = 0;
      $arr = explode('|', $object[$oCnt]);
      list($oType, $oType2) = explode(':', $arr[0]);
      list($oWidth, $oHeight, $lineSize) = explode(';', $arr[1]);
      list($lineColor, $fillColor) = explode(';', $arr[2]);
      list($hAlign, $vAlign) = explode(';', $arr[3]);
      list($lMargin, $rMargin, $tMargin, $bMargin) = explode(';', $arr[4]);
      $rotation = $arr[5];
      $opacity = $arr[6];
      $text = trim($arr[7]);

      if($lMargin >= $width) $lMargin = 0;
      if($rMargin >= $width) $rMargin = 0;
      if($lMargin + $rMargin >= $width) $rMargin = $lMargin = 0;
      if($tMargin >= $height) $tMargin = 0;
      if($bMargin >= $height) $bMargin = 0;
      if($tMargin + $bMargin >= $height) $tMargin = $bMargin = 0;

      list($oID, $oBGCol, $lineCol, $fillCol) = create_image($width+1, $height+2, $alphaColor, $lineColor, $fillColor, 127);

      if($oID) {

        switch($oType) {

          case 'image':
            if($text) {
              $error = object_image($oID, $oWidth, $oHeight, $rotation, $text);
              if($error) $message[] = "$oCnt: $error";
            }
            else $message[] = "$oCnt: Choose image";
            break;

          case 'ellipse':
            $error = object_ellipse($oID, $oWidth, $oHeight, $rotation);
            if($error) $message[] = "$oCnt: $error";
            break;

          case 'poly':
            $error = object_poly($oID, $oWidth, $oHeight, $rotation, $oType2);
            if($error) $message[] = "$oCnt: $error";
            break;

          case 'text':
            if(!$oType2) $message[] = "$oCnt: Choose font";
            else if(!$text) $message[] = "$oCnt: Enter text";
            else {
              $error = object_text($oID, $oType2, $oWidth, $rotation, $text);
              if($error) $message[] = "$oCnt: $error";
            }
            break;

          default: $message[] = "$oCnt: Choose object type"; break;
        }
        copy_image($mainID, $oID, $oBGCol, $opacity);
        imageDestroy($oID);
      }
    }

    if(count($message)) view_message($mainID, $message);

    if($file) view_image($type, $mainID, $file);
    else {
      header('Content-type: image/' . strtolower($type));
      header('Cache-control: private, no-cache, must-revalidate');
      header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');
      header('Date: Sat, 01 Jan 2000 00:00:00 GMT');
      header('Pragma: no-cache');
      view_image($type, $mainID);
    }
    imageDestroy($mainID);
  }
?>
Return current item: Image Maker