HomeBlogMagic

PHP simple Image editing class

Aktuell habe ich eine kleine Image Klasse im Einsatz die hauptsächlich dazu gedacht ist Images zu laden, größe ändern und convertieren.
Die Klasse lässt sich leicht um mehr Features erweitern. Bisher ist der Funktionsumfang für mich ausreichend.

Hier die Klasse (CcImage.php):

<?php
class CcImage
{
  private $m_sPath;
  private $m_oInfo;
  private $m_oImage;
  private $m_iWidth;
  private $m_iHeight;
  private $m_bImageValid;

  public function __construct($sPath)
  {
    $this->m_sPath = $sPath;
    $this->loadImage();
  }

  public function isImage()
  {
    return $this->m_bImageValid;
  }

  public function loadImage()
  {
    $this->m_bImageValid = false;
    if(is_file($this->m_sPath))
    {
      $finfo = finfo_open(FILEINFO_MIME_TYPE); // gib den MIME-Typ ala mimetype-Erweiterung zurück
      if($finfo)
      {
        $info = finfo_file($finfo, $this->m_sPath);
        switch ($info)
        {
          case "image/jpeg":
          case "image/jpg":
            $this->m_oImage = imagecreatefromjpeg ( $this->m_sPath );
            $size = getimagesize( $this->m_sPath );
            $this->m_iWidth = $size[0];
            $this->m_iHeight = $size[1];
            $this->m_bImageValid = true;
            break;
          case "image/png":
            $this->m_oImage = imagecreatefrompng( $this->m_sPath );
            $size = getimagesize( $this->m_sPath );
            $this->m_iWidth = $size[0];
            $this->m_iHeight = $size[1];
            $this->m_bImageValid = true;
            break;
          default:
            //echo $info."\n";
        }
        finfo_close($finfo);
      }
    }
    return $this->m_bImageValid;
  }

  public function resize($x, $y, $bKeepRatio = true)
  {
    $bSuccess = false;
    if (extension_loaded('gd'))
    {
      if($bKeepRatio)
      {
        $width = $this->m_iWidth/$x;
        $height = $this->m_iHeight/$y;
        if ($width > $height) $resize = $width;
        else                  $resize = $height;
        $newwidth = $this->m_iWidth/$resize;
        $newheight = $this->m_iHeight/$resize;
      }
      else
      {
        $newwidth = $x;
        $newheight = $y;
      }

      $newPic = imagecreatetruecolor ( (int)$newwidth, (int)$newheight );
      if(imagecopyresampled ( $newPic, $this->m_oImage, 0, 0, 0, 0, (int)$newwidth, (int)$newheight, $this->m_iWidth, $this->m_iHeight))
      {
        $this->m_oImage = $newPic;
        $this->m_iWidth = $newwidth;
        $this->m_iHeight = $newheight;
        $bSuccess = true;
      }
      else
      {
        echo "imagecopyresampled failed\n";
      }
    }
    return $bSuccess;
  }

  public function convertToPng($sTarget)
  {
    $bSuccess = false;
    if (extension_loaded('gd'))
    {
      $bSuccess = imagepng($this->m_oImage, $sTarget);
    }
    return $bSuccess;
  }

  public function convertToJpg($sTarget)
  {
    $bSuccess = false;
    if (extension_loaded('gd'))
    {
      $bSuccess = imagejpeg($this->m_oImage, $sTarget);
    }
    return $bSuccess;
  }

  public static function resize_img($path, $target, $x, $y)
  {
    $oImage = new CcImage("$path");
    if($oImage->isImage())
    {
      if($oImage->resize($x, $y))
      {
        $oImage->convertToPng("$target");
      }
    }
  }
}

Beispiel

Hier ein kleines Beispiel mit der ich die Funktion Klasse in der Kommandozeile getestet habe:

<?php 
include("CcImage.php");

$oImage = new CcImage("sample.png");
if($oImage->isImage())
{
  if($oImage->resize(100, 100))
  {
    $oImage->convertToJpg("sample.out.jpg");
    $oImage->convertToPng("sample.out.png");
  }
  else
  {
    echo "Resize failed\n";
  }
}
else
{
  echo "Failed to load image\n";
}
Permalink: https://adirmeier.de/Blog/ID_430
Tags: Blog, img, jpeg, jpg, php, pngvon am 2022-10-12