PHP Nach abarbeitung einer Klasse beendet sich script.

Chakky

Member of Honour
//ok gelöst nach langen probieren

hab meine dateien jetzt auf maximal 8 mb beschränkt mehr lässt mein anbieter beim hochladen eh nicht zu.Obwohl maximal 64MB Memorylimit für in der php.ini festgelegt sind.

hab mich noch einen kleinen trick bedient und zwar lass ich das Bild erst kleiner machen bevor ich das Watermark setze. Jetzt läuft er nicht mehr in ein Limit rein

//lösung ende

Hallo,

ich hab jetzt folgendes Problem:
Ich habe mir aus diesen Thread: http://www.hackerboard.de/web-desig...che-kleine-php-und-javascript-funktionen.html

die Imageclass genommen:
inhalt der image.class.php
PHP:
<?php
//error_reporting(E_ALL);
/**
 * image-object, where you can edit the image (resize / rotate)
 * works with gdlib!
 * @author Martin Bergann <martin@cs-bergann.de>
 *
 */
class Image {

    private $_path;
    private $_img = null;
    private $_infos = null;
    private $_error = array();
    private $_exif = array();
    private $_error_msg = array(
        '0' => '',
        '1' => 'error while loading image',
        '2' => 'error while saving image',
        '3' => 'invalid dimensions',
        '4' => 'no image loaded'
    );


    /* Constructor / Destructor */

    public function __construct($path='') {
        if (!empty($path))
            $this->loadImage($path);
    }

    /* load and save */

    public function loadImage($path) {
        $this->_path = $path;
        if (!$this->_infos = @getimagesize($path)) {
            array_push($this->_error, 1);
            return false;
        }

        //    var_dump($this->_infos);

        if ($this->_infos[2] == 1) {
            // Bild ist vom Typ gif
            $this->_img = @imagecreatefromgif($this->_path);
        } elseif ($this->_infos[2] == 2) {
            // Bild ist vom Typ jpg
            $this->_img = @imagecreatefromjpeg($this->_path);
            $this->_exif = exif_read_data($this->_path, false, true);
        } elseif ($this->_infos[2] == 3) {
            // Bild ist vom Typ png
            $this->_img = @imagecreatefrompng($this->_path);
        } else {
            array_push($this->_error, 2);
            return false;
        }

        return true;
    }

    /**
     * saves images by filename-extension
     */
    public function saveImage($path, $quality = 90) {
        $dir = dirname($path);
        $dirName = basename($dir);
        $parentDir = dirname($dir);
        if (!file_exists($dir)) {
            if (!is_dir($parentDir) || !is_writable($parentDir)) {
                array_push($this->_error, 2);
                return false;
            }
            mkdir($dir);
        } elseif (!is_dir($dir) || !is_writable($dir)) {
            array_push($this->_error, 2);
            return false;
        }

        switch (strtolower($this->getFilenameExtension($path))) {
            case 'jpg':
            case 'jpeg':
                if (!@imagejpeg($this->_img, $path, $quality)) {
                    array_push($this->_error, 2);
                    return false;
                }
                break;
            case 'gif':
                if (!@imagegif($this->_img, $path)) {
                    array_push($this->_error, 2);
                    return false;
                }
                break;
            case 'png':
                if (!@imagepng($this->_img, $path, $quality)) {
                    array_push($this->_error, 2);
                    return false;
                }
                break;
            default:
                break;
        }
        $this->loadImage($path);
        return true;
    }

    public function getExif() {
        return $this->_exif;
    }

    public function generateImage($width, $height, $color) {
        $this->_img = imagecreatetruecolor($width, $height);
        imagefill($this->_img, 0, 0, $color);
    }

    public function createTextImage($text, $fontfile, $fontsize, $text_color=0xFFFFFF, $background_color=0, $fontangle=0) {
        if (!is_numeric($fontsize)) {
            $fontsize = 20;
        }
        if (!is_numeric($fontangle)) {
            $fontangle = 0;
        }

        //get size of text-field
        $textfield = imagettfbbox($fontsize, $fontangle, $fontfile, $text);
        $text_size_x = (abs($textfield[4] - $textfield[0]) + 10);
        $text_size_y = (abs($textfield[1] - $textfield[7]) + 10);
        $text_pos_x = 3;
        $text_pos_y = $fontsize + 5;

        $text_picture = imagecreatetruecolor($text_size_x, $text_size_y);

        $t_red = ($text_color >> 16) & 0xFF;
        $t_green = ($text_color >> 8) & 0xFF;
        $t_blue = ($text_color) & 0xFF;

        $b_red = ($background_color >> 16) & 0xFF;
        $b_green = ($background_color >> 8) & 0xFF;
        $b_blue = ($background_color) & 0xFF;

        $background_color = imagecolorallocate($text_picture, $b_red, $b_green, $b_blue);
        $text_color = imagecolorallocate($text_picture, $t_red, $t_green, $t_blue);

        imagefill($text_picture, 0, 0, $background_color);
        imagettftext($text_picture, $fontsize, $fontangle, $text_pos_x, $text_pos_y, $text_color, $fontfile, $text);
        //if(isset($this->_img))imagedestroy($this->_img);
        $this->_img = $text_picture;
    }

    public function getImageRessource() {
        return $this->_img;
    }

    /* get infos */


 
    public function getOrigWidth() {
        return $this->_infos[0];
    }

    public function getOrigHeight() {
        return $this->_infos[1];
    }

    public function getCurrentWidth() {
        return (!is_null($this->_img)) ? imagesx($this->_img) : 0;
    }

    public function getCurrentHeight() {
        return (!is_null($this->_img)) ? imagesy($this->_img) : 0;
    }

    public function getOrigMime() {
        return $this->_infos['mime'];
    }

    public function getOrigFilenameExtension() {
        return $this->getFilenameExtension($this->_path);
    }

    protected function getFilenameExtension($path) {
        return preg_replace('/^.*\.([0-9a-z]+)$/i', '\\1', $path);
    }

    /* Errors */

    public function hasError() {
        return (sizeof($this->_error) != 0);
    }

    public function getErrorMsg() {
        $r = '';
        foreach ($this->_error as $errno) {
            $r.='Error ' . $errno . ' : ' . $this->_error_msg[$errno] . chr(10);
        }
        return $r;
    }

    /* manipulate image */

    public function rotate($degree, $bg=0) {
        try {
            $img = imagerotate($this->_img, $degree, $bg);
        } catch (Exception $e) {
            echo $e;
        }

        imagedestroy($this->_img);
        $this->_img = $img;
    }

    /**
     * mode:
     * 0 = use long side to scale and override short side
     * 1 = use short side to scale and override long side
     * 2 = use width to scale and override height
     * 3 = use height to scale and override width
     * --
     * 4 = use width and height - add a black (or any other color) background to fill
     * 5 = use width and height - zoom to the center and cut the rest of the image
     */
    public function resizeProportional($width, $height, $mode=0, $bgcol=0) {
        $new_w = 0;
        $new_h = 0;

        if ($mode == 5) {
            $img = imagecreatetruecolor($width, $height);
            imagefill($img, 0, 0, $bgcol);
            $ratio = $this->getCurrentHeight() / $this->getCurrentWidth();
            $new_ratio = $height / $width;
            if ($ratio < $new_ratio) {
                //cut left and right
                $new_h = $height;
                $new_w = $height / $ratio;
                $diff = ($width - $new_w);
                imagecopyresampled($img, $this->_img, $diff / 2, 0, 0, 0, $width - $diff, $height, $this->getCurrentWidth(), $this->getCurrentHeight());
            } else {
                //cut top and bottom
                $new_w = $width;
                $new_h = $width * $ratio;
                $diff = ($height - $new_h);
                imagecopyresampled($img, $this->_img, 0, $diff / 2, 0, 0, $width, $height - $diff, $this->getCurrentWidth(), $this->getCurrentHeight());
            }
            imagedestroy($this->_img); /* nötig? */
            $this->_img = $img;
        } elseif ($mode == 4) {
            $img = imagecreatetruecolor($width, $height);
            imagefill($img, 0, 0, $bgcol);
            $ratio = $this->getCurrentHeight() / $this->getCurrentWidth();
            $new_ratio = $height / $width;
            if ($new_ratio > $ratio) {
                //border at top and bottom
                $new_w = $width;
                $new_h = $width * $ratio;
                $diff = ($height - $new_h);
                imagecopyresampled($img, $this->_img, 0, $diff / 2, 0, 0, $width, $height - $diff, $this->getCurrentWidth(), $this->getCurrentHeight());
            } else {
                //border left and right
                $new_h = $height;
                $new_w = $height / $ratio;
                $diff = ($width - $new_w);
                imagecopyresampled($img, $this->_img, $diff / 2, 0, 0, 0, $width - $diff, $height, $this->getCurrentWidth(), $this->getCurrentHeight());
            }
            imagedestroy($this->_img); /* nötig? */
            $this->_img = $img;
        } elseif ($mode == 3 || ($mode == 0 && $this->getCurrentHeight() >= $this->getCurrentWidth()) || ($mode == 1 && $this->getCurrentHeight() <= $this->getCurrentWidth())) {
            $ratio = $this->getCurrentWidth() / $this->getCurrentHeight();
            $new_h = $height;
            $new_w = $ratio * $height;
            $this->resize($new_w, $new_h);
        } else {
            $ratio = $this->getCurrentHeight() / $this->getCurrentWidth();
            $new_w = $width;
            $new_h = $ratio * $width;
            $this->resize($new_w, $new_h);
        }
    }

    public function resize($width, $height) {
        if (!is_numeric($width) || $width <= 0 || !is_numeric($height) || $height <= 0) {
            array_push($this->_error, 3);
            return false;
        }
        $img = imagecreatetruecolor($width, $height);
        imagecopyresampled($img, $this->_img, 0, 0, 0, 0, $width, $height, $this->getCurrentWidth(), $this->getCurrentHeight());
        imagedestroy($this->_img); /* nötig? */
        $this->_img = $img;
    }

    public function crop($top, $left, $bottom, $right) {
        $nw = ($right - $left);
        $nh = ($bottom - $top);
        $img = imagecreatetruecolor($nw, $nh);
        imagecopyresampled($img, $this->_img, 0, 0, $left, $top, $nw, $nh, $nw, $nh);
        imagedestroy($this->_img);
        $this->_img = $img;
    }

    public function setTransparentColor($transparentColor, $toleranz=10) {
        if (is_null($this->_img)) {
            array_push($this->_error, 4);
            return false;
        }
        //split index to red green and blue:
        $t_red = ($transparentColor >> 16) & 0xFF;
        $t_green = ($transparentColor >> 8) & 0xFF;
        $t_blue = ($transparentColor) & 0xFF;
        /* info:
         *     combining the three colors to the color-index:
         *     $transparentColor=($t_red<<16)|($t_green<<8)|($t_blue);
         */

        //fill all pixel within a defined tolerance with the transparentColor
        for ($ix = 0; $ix < $this->getCurrentWidth(); $ix++) {
            for ($iy = 0; $iy < $this->getCurrentHeight(); $iy++) {
                $farbindex = imagecolorat($this->_img, $ix, $iy);
                $p_red = ($farbindex >> 16) & 0xFF;
                $p_green = ($farbindex >> 8) & 0xFF;
                $p_blue = ($farbindex) & 0xFF;
                if (($p_red >= ($t_red - $toleranz) && $p_red <= $t_red + $toleranz) && ($p_green >= ($t_green - $toleranz) && $p_green <= $t_green + $toleranz) && ($p_blue >= ($t_blue - $toleranz) && $p_blue <= $t_blue + $toleranz)) {
                    imagesetpixel($this->_img, $ix, $iy, $transparentColor);
                }
            }
        }

        imagecolortransparent($this->_img, $transparentColor);
    }

    public function addWatermarkImageObject($ImageObject, $opacity=50, $posX=null, $posY=null) {
        if (is_null($posX))
            $posX = $this->getCurrentWidth() / 2 - ($ImageObject->getCurrentWidth() / 2);
        if (is_null($posY))
            $posY = $this->getCurrentHeight() / 2 - ($ImageObject->getCurrentHeight() / 2);
        if (is_null($this->_img)) {
            array_push($this->_error, 4);
            return false;
        }
        imagecopymerge($this->_img, $ImageObject->getImageRessource(), $posX, $posY, 0, 0, $ImageObject->getCurrentWidth(), $ImageObject->getCurrentHeight(), $opacity);
    }

    public function addWatermarkImageRessource($Image, $opacity=50, $posX=null, $posY=null) {
        if (is_null($posX))
            $posX = $this->getCurrentWidth() / 2 - (imagesx($Image) / 2);
        if (is_null($posY))
            $posY = $this->getCurrentHeight() / 2 - (imagesx($Image) / 2);
        if (is_null($this->_img)) {
            array_push($this->_error, 4);
            return false;
        }
        imagecopymerge($this->_img, $Image, $posX, $posY, 0, 0, imagesx($Image), imagesx($Image), $opacity);
    }

    public function addWatermark($watermarkFile, $opacity=50, $posX=null, $posY=null, $angle=0, $transparentColor=null, $resizeW=null, $resizeH=null, $resizeMode=null) {

        $watermark = new Image($watermarkFile);

        if (is_null($resizeMode)) {
            if (!is_null($resizeW) && !is_null($resizeH))
                $watermark->resize($resizeW, $resizeH);
        }
        else {
            if (!is_null($resizeW) && !is_null($resizeH))
                $watermark->resizeProportional($resizeW, $resizeH, $resizeMode);
        }
        //im Fehler-Fall beenden
        if ($watermark->hasError())
            return false;

        $watermark->rotate($angle, $transparentColor);
        $watermark->setTransparentColor($transparentColor);
        $this->addWatermarkImageObject($watermark, $opacity, $posX, $posY);
    }

    public function showJPG() {
        if (is_null($this->_img)) {
            array_push($this->_error, 4);
            return false;
        }
        header('Content-type: image/jpeg');
        imagejpeg($this->_img);
    }

    public function showPNG() {
        if (is_null($this->_img)) {
            array_push($this->_error, 4);
            return false;
        }
        header('Content-type: image/png');
        imagepng($this->_img);
    }

}

Die habe ich jetzt mal Testweise in meinen php Script eingebaut:
Mein Code:
PHP:
<?php
error_reporting(E_ALL);
include('image.class.php');

$orig = "1140.jpg";




$img = new Image($orig);

$img->addWatermark(
        'watermark.jpg', //watermark-image
        100, //opacity
        NULL, //pos x
        NULL, //pos y
        0, // angle
        0xFFFFFF // transparent color
);
echo "vor save";
$img->saveImage($orig);

echo "nach save";
?>

<img src="1140.jpg">

Das Script soll nur auf das Bild ein Watermark setzen. Folgendes Problem tritt jetzt auf:
alles was nach
Code:
$img->saveImage($orig);
wird nicht mehr ausgeführt. PHP bringt mir keinerlei fehlermeldungen und in den Logfiles ist auch nix zu sehen.

Ich konnte aber feststellen das "saveImage" noch ausgeführt wird. Bei manchen Dateien (von der größe unterschiedlich und kein System festzustellen) wird das Script weiterhin ausgeführt bei anderen nicht, aber jedesmal wird trotzdem noch ein Watermark gesetzt.

Kann mir jemand erklären warum und vorallem wie ich das beheben kann?

ich hab es mit
PHP:
 try { } und catch() {}
versucht aber damit kann man die dinge nicht abfangen.

Danke

//update:

nach dem überall mal das @ entfernt habe in der klasse gibts auch ne fehlermedlung:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 2460 bytes)

Ok es läuft in ein Speicherlimit ich versuch dann mal das limit per php.ini hochzusetzen, wenn das nicht klappt sonst jemand eine Idee?
 
Zuletzt bearbeitet:
Zurück
Oben