apache android asp.net codeigniter firefox linux php
python windows

PHP ile yazılmış Image Sınıfı

Gönderen : Mustafa İleri Tarih : March 12 | 22:36

Efendim uzuun bir aradan sonra kaldığımız yerden devam edelim.Geçen bir arkadaşımın isteği üzerine bir image Upload sınıfı yazdım.Hani fena da olmadı iş görüo gibi. Metod açıklamalarını falan henüz yazmadım ama paylaşayım dedim.Bir de BMP yi eklemeyi unutmuşum ekstra özellikler ile geliştirilebilir.Orta düzeyde işinizi görebileceğini düşünüyorum

<?php

class ImageUpload {
    protected $imageMaxSize = 2000000;
    protected $imageAvailableTypes =    array(
                                                1 => "image/png",
                                                2 => "image/x-png",
                                                3 => "image/jpeg",
                                                4 => "image/pjpeg",
                                                5 => "image/gif"
                                              );

    public $currentImageType;
    public $imageName;
    public $imageType;
    public $imageSize;
    public $imageTmpName;
    public $destination;
    public $errors = array();
    public $imageWidth;
    public $imageHeight;

    function __construct() {

    }
    function uploadImage($image) {
        $this->setImageData($image);
        $this->controlImageData();
        $this->controlDataSize();
        $this->saveImage();
    }

    function setImageData($image) {
        $this->imageName = $image["name"];
        $this->imageType = $image["type"];
        $this->imageTmpName = $image["tmp_name"];
        $this->imageSize = $image["size"];
    }

    function controlImageData() {
        $this->controlDataType();
        $this->controlDataSize();
    }

    function controlDataType() {
        $result = array_search($this->imageType, $this->imageAvailableTypes);
        if ($result == false) {
            array_push($this->errors, "type");
            return false;
        }
        $this->currentImageType = $result;
        return true ;
    }

    function controlDataSize() {
        if ($this->imageSize > $this->imageMaxSize) {
            array_push($this->errors, "size");
            return false;
        }
        return true;
    }

    function saveImage() {
        $imageType = $this->currentImageType;

        //$this->destination = $this->imageName;
        //$this->imageWidth = 200;
        //$this->imageHeight = 200;
        move_uploaded_file($this->imageTmpName, $this->destination);
        $pointapi = getimagesize($this->destination);
        $currentW = $pointapi[0];
        $currentH = $pointapi[1];

        if ($imageType == 1 or $imageType == 2) {
            $image = imagecreatefrompng($this->destination);
        } elseif ($imageType == 3 or $imageType == 4) {
            $image = imagecreatefromjpeg($this->destination);
        } elseif ($imageType == 5) {
            $image = imagecreatefromgif($this->destination);
        }


        if ($currentH > $currentW) {
            $newH = $this->imageHeight;
            if ($newH > $currentH) {
                $newH = $currentH;
                $newW = $currentW;
            } else {
                $newW = ($newH/$currentH) * $currentW;
            }
        } else{
            $newW = $this->imageWidth;
            if ($newW > $currentW) {
                $newH = $currentH;
                $newW = $currentW;
            } else {
                $newH = ($newW/$currentW) * $currentH;
            }
        }
        $newImage = imagecreatetruecolor($newW, $newH);
        imagealphablending($newImage, false);
        imagesavealpha($newImage,true);


        $black = imagecolorallocate($newImage, 0, 0, 0);
        imagecolortransparent($newImage, $black);


        imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newW, $newH, $currentW, $currentH);

        if ($imageType == 1 or $imageType == 2) {
            imagepng($newImage, $this->destination, 0);
        } elseif ($imageType == 3 or $imageType == 4) {
            imagejpeg($newImage, $this->destination, 100);
        } elseif ($imageType == 5) {
            imagegif($newImage, $this->destination);
        }

        imagedestroy($newImage);
    }
}

Basit bir kullanım şekli

$teamLogo = $_FILES['teamLogo'];
            $uploadFile = "data/image.xxx";
            $imageUploader = new ImageUpload();
            $imageUploader->destination = $uploadFile;
            $imageUploader->imageWidth = 50;
            $imageUploader->imageHeight = 60;
            $imageUploader->uploadImage($teamLogo);

Cevap Yaz:



Ad Soyad :    E-Posta : (Asla gösterilmez)