Edit: Klasa została nominowana do nagrody miesiąca,
zachęcam do głosowania:
http://www.phpclasses.org/vote.html



Edit: Klasa znalazła swoje miejsce tu: http://www.phpclasses.org/browse/package/5181.html

Witam.

Potrzebowałem skryptu który będzie usuwał ramkę ze zdjęć (np. źle wycięte zdjęcie po skanowaniu).
Nie znalazłem niczego gotowego, więc napisałem sam.
Mam nadzieję, że komuś się przyda.

Uwagi jak najbardziej mile widziane.


Klasa:
  1. <?php
  2. /*******************************************************************************
  3. * Software: removeBoderFromImage                                               *
  4. * Version:  1.0                                                                *
  5. * Date:     2009-02-19                                                         *
  6. * Author:   Pawel Antczak                                                      *
  7. * License:  Freeware                                                           *
  8. *                                                                              *
  9. * You may use, modify and redistribute this software as you wish.              *
  10. *******************************************************************************/
  11. class removeBoderFromImage {
  12.    /* Original image */
  13.    private $image;
  14.    /* Image MIME type */
  15.    private $imageType;
  16.    /* Peak border color*/
  17.    private $peakColor;
  18.    /* Image height */
  19.    private $imageHeight;
  20.    /* Image weight */
  21.    private $imageWidth;
  22.    /*Image name*/
  23.    private $imageName;
  24.    /* Cropped image*/
  25.    private $croppedImage;
  26.    /** Class constructor
  27.     * @param string $image - original image path/URL
  28.     * @param string $colorFactor - color mulitiplier; lower factor = smaller crop area
  29.     */
  30.    public function __construct($image, $colorMultiplier = 0.95) {
  31.        $this->imageName = $image;
  32.        $this->image = imagecreatefromstring(file_get_contents($image));
  33.        $imageDetails = GetImageSize($image);
  34.        $this->imageHeight = $imageDetails[1];
  35.        $this->imageWidth = $imageDetails[0];
  36.        $this->imageType = $imageDetails['mime'];
  37.        $this->peakColor = $this->getPeakColor() * $colorMultiplier;
  38.        $this->removeBorder();
  39.    }
  40.    /*
  41.      * Internal function
  42.      * Calculates crop area dimension
  43.      */
  44.    private function removeBorder() {
  45.        $newStartX = $this->imageHeight;
  46.        $newStartY = $this->imageWidth;
  47.        $newStopX = 0;
  48.        $newStopY = 0;
  49.        for ($i = 0 ; $i < $this->imageWidth ; $i++) {
  50.            for ($ii = 0 ; $ii < $this->imageHeight ; $ii++) {
  51.                if ($this->getPixelColor($this->image, $i, $ii) < $this->peakColor) {
  52.                    if ($i > $newStopX) $newStopX = $i;
  53.                    if ($ii > $newStopY) $newStopY = $ii; }
  54.            }
  55.        }
  56.        for ($i = 0 ; $i < $this->imageWidth ; $i++) {
  57.            for ($ii = 0 ; $ii < $this->imageHeight ; $ii++) {
  58.                if ($this->getPixelColor($this->image, $i, $ii) < $this->peakColor) {
  59.                    if ($i < $newStartX) $newStartX = $i;
  60.                    if ($ii < $newStartY) $newStartY = $ii; }
  61.            }
  62.        }
  63.        $this->cropImage($newStartX,$newStartY,$newStopX,$newStopY);
  64.    }
  65.    /*
  66.      * Internal function
  67.      * Returns color at pixel
  68.      */
  69.    private function getPixelColor($image, $x, $y) {
  70.        return imagecolorat($image, $x, $y);
  71.    }
  72.    /*
  73.      * Internal function
  74.      * Copy area from image to new one
  75.      */
  76.    private function cropImage($newStartX, $newStartY, $newStopX, $newStopY) {
  77.        $newwidth = $this->imageWidth;
  78.        $newheight = $this->imageHeight;
  79.        $cropped = imagecreatetruecolor($newStopX - $newStartX, $newStopY - $newStartY);
  80.        imagecopyresized($cropped, $this->image, 0, 0, $newStartX, $newStartY, $newStopX - $newStartX, $newStopY - $newStartY, $newStopX - $newStartX, $newStopY - $newStartY);
  81.        $this->croppedImage = $cropped;
  82.    }
  83.    /*
  84.      * Internal function
  85.      * Retuns image avarage color
  86.      */
  87.    private function getPeakColor() {
  88.        $palette = array();
  89.        for ($i = 0 ; $i < $this->imageWidth ; $i++) {
  90.            for ($ii = 0 ; $ii < $this->imageHeight ; $ii++) {
  91.                $palette[] += $this->getPixelColor($this->image, $i, $ii);
  92.            }
  93.        }
  94.        return round(array_sum($palette)/count($palette));
  95.    }
  96.    /*
  97.      * Removes border and send image to browser
  98.      */
  99.    public function showImage() {
  100.        header('Content-type: image/jpeg');
  101.        imagejpeg($this->croppedImage);
  102.    }
  103.    /*
  104.      * Removes border and save file
  105.      * @param string $newImagePath - path to save new file
  106.      * @param string $imageType - \"jpg\", \"gif\", \"png\"
  107.      */
  108.    public function saveImage($newImagePath, $imageType = &#092;"jpg\") {
  109.        $newFileNameTemp = explode(&#092;".\",$newImagePath);
  110.        $newFileName = $newFileNameTemp[0].&#092;".\".$imageType;
  111.        switch($imageType) {
  112.            case &#092;"jpg\":
  113.                imagejpeg($this->croppedImage,$newFileName);
  114.                break;
  115.            case &#092;"png\":
  116.                imagepng($this->croppedImage,$newFileName);
  117.                break;
  118.            case &#092;"gif\":
  119.                imagegif($this->croppedImage,$newFileName);
  120.                break;
  121.            default:
  122.                imagejpeg($this->croppedImage,$newFileNameTemp[0].&#092;".jpg\");
  123.            }
  124.        }
  125.  
  126.    /*
  127.      * Removes border and returns image
  128.      */
  129.    public function getImage() {
  130.        return $this->croppedImage;
  131.    }
  132. }
  133. ?>


Użycie klasy:
  1. <?php
  2. require &#092;"removeBorderFromImage.php\";
  3. $rbfi = new removeBoderFromImage(&#092;"img.jpg\");
  4. $cropped = $rbfi->getImage();                   //zwraca wycinek
  5. $rbfi->saveImage(&#092;"c:wycinek\", \"png\");       //zapisuje plik na dysku
  6. $rbfi->showImage();                             //wysyla wycinek do przegladarki
  7. ?>