Skip to main content
deleted 63 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am working on aan image manipulation class for a project to re-size uploaded slider images to 780 X 397 and uploaded avartaravatar pictures to 220 X 220. I am wondering if this is a good way to construct it.

A question I have is whatWhat would be the best way to handle an image that is smaller then 780 X 397. Should I simply re-size it or go about it a different way?

HaveI have included the class code as well as the code syntax on how it is to be used:

    class imageEditer{
    private $filename;
    public $picWidth;
    public $picHeight;
    public $picMimeType;
    public $picMimeTypeCons;
    private $allowedMimeType = array(IMAGETYPE_GIF=>'.gif',IMAGETYPE_JPEG=>'.jpg',IMAGETYPE_PNG=>'.png',IMAGETYPE_BMP=>'.bmp');
    public $picture;
    private $imageWork;
    private $storePath;
    
    public function __construct($filename){
        if(is_file($filename)){
            $this->filename = $filename;
            $getimagesize = getimagesize($this->filename);
            $this->picWidth = $getimagesize[0];
            $this->picHeight = $getimagesize[1];
            $this->picMimeTypeCons = $getimagesize[2];
            if($this->allowedMime()){
                $this->picMimeType = image_type_to_mime_type($this->picMimeTypeCons);
                $this->picture = $this->imageCreate($this->picMimeTypeCons);
            }else{
                trigger_error('Mime type not allowed',E_USER_ERROR);
            }
        }else{
            trigger_error('File not found',E_USER_ERROR);
        }
    }
    
    public function imageCreate($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagecreatefromgif($this->filename);
                break;
            case IMAGETYPE_JPEG:
                return imagecreatefromjpeg($this->filename);
                break;
            case IMAGETYPE_PNG:
                return imagecreatefrompng($this->filename);
                break;
            case IMAGETYPE_BMP:
                return imagecreatefromwbmp($this->filename);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function imageCreateSlider($storePath,$width = 780,$height = 397){
        $this->storePath = $storePath;
        $divWidth = floor(($this->picWidth-$width)/2) ;
        $divHeight = floor(($this->picHeight-$height)/2);
        $this->imageWork = imagecrop($this->picture,array("x"=>$divWidth,"y"=>$divHeight,"width"=>$width,"height"=>$height));
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageAvatar($storePath,$width = 220,$height = 220){
        $this->storePath = $storePath;
        $this->imageWork = imagescale($this->picture,$width,$height);
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageCreateType($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagegif($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_JPEG:
                return imagejpeg($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_PNG:
                return imagepng($this->imageWork,$this->storePath,9,PNG_ALL_FILTERS);
                break;
            case IMAGETYPE_BMP:
                return image2wbmp($this->imageWork,$this->storePath);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function allowedMime(){
        for ($i=0; $i <count($this->allowedMimeType) ; $i++) {
            return array_key_exists($this->picMimeTypeCons,$this->allowedMimeType);
        }
    }
    public function __destruct(){
        imagedestroy($this->picture);
        if(!empty($this->imageWork)) imagedestroy($this->imageWork);
    }
}

Any suggestion are appreciated.

I am working on a image manipulation class for a project to re-size uploaded slider images to 780 X 397 and uploaded avartar pictures to 220 X 220. I am wondering if this is a good way to construct it.

A question I have is what would be the best way to handle an image that is smaller then 780 X 397. Should I simply re-size it or go about it a different way?

Have have included the class code as well as the code syntax on how it is to be used

    class imageEditer{
    private $filename;
    public $picWidth;
    public $picHeight;
    public $picMimeType;
    public $picMimeTypeCons;
    private $allowedMimeType = array(IMAGETYPE_GIF=>'.gif',IMAGETYPE_JPEG=>'.jpg',IMAGETYPE_PNG=>'.png',IMAGETYPE_BMP=>'.bmp');
    public $picture;
    private $imageWork;
    private $storePath;
    
    public function __construct($filename){
        if(is_file($filename)){
            $this->filename = $filename;
            $getimagesize = getimagesize($this->filename);
            $this->picWidth = $getimagesize[0];
            $this->picHeight = $getimagesize[1];
            $this->picMimeTypeCons = $getimagesize[2];
            if($this->allowedMime()){
                $this->picMimeType = image_type_to_mime_type($this->picMimeTypeCons);
                $this->picture = $this->imageCreate($this->picMimeTypeCons);
            }else{
                trigger_error('Mime type not allowed',E_USER_ERROR);
            }
        }else{
            trigger_error('File not found',E_USER_ERROR);
        }
    }
    
    public function imageCreate($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagecreatefromgif($this->filename);
                break;
            case IMAGETYPE_JPEG:
                return imagecreatefromjpeg($this->filename);
                break;
            case IMAGETYPE_PNG:
                return imagecreatefrompng($this->filename);
                break;
            case IMAGETYPE_BMP:
                return imagecreatefromwbmp($this->filename);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function imageCreateSlider($storePath,$width = 780,$height = 397){
        $this->storePath = $storePath;
        $divWidth = floor(($this->picWidth-$width)/2) ;
        $divHeight = floor(($this->picHeight-$height)/2);
        $this->imageWork = imagecrop($this->picture,array("x"=>$divWidth,"y"=>$divHeight,"width"=>$width,"height"=>$height));
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageAvatar($storePath,$width = 220,$height = 220){
        $this->storePath = $storePath;
        $this->imageWork = imagescale($this->picture,$width,$height);
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageCreateType($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagegif($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_JPEG:
                return imagejpeg($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_PNG:
                return imagepng($this->imageWork,$this->storePath,9,PNG_ALL_FILTERS);
                break;
            case IMAGETYPE_BMP:
                return image2wbmp($this->imageWork,$this->storePath);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function allowedMime(){
        for ($i=0; $i <count($this->allowedMimeType) ; $i++) {
            return array_key_exists($this->picMimeTypeCons,$this->allowedMimeType);
        }
    }
    public function __destruct(){
        imagedestroy($this->picture);
        if(!empty($this->imageWork)) imagedestroy($this->imageWork);
    }
}

Any suggestion are appreciated.

I am working on an image manipulation class for a project to re-size uploaded slider images to 780 X 397 and uploaded avatar pictures to 220 X 220. I am wondering if this is a good way to construct it.

What would be the best way to handle an image that is smaller then 780 X 397. Should I simply re-size it or go about it a different way?

I have included the class code as well as the code syntax on how it is to be used:

class imageEditer{
    private $filename;
    public $picWidth;
    public $picHeight;
    public $picMimeType;
    public $picMimeTypeCons;
    private $allowedMimeType = array(IMAGETYPE_GIF=>'.gif',IMAGETYPE_JPEG=>'.jpg',IMAGETYPE_PNG=>'.png',IMAGETYPE_BMP=>'.bmp');
    public $picture;
    private $imageWork;
    private $storePath;
    
    public function __construct($filename){
        if(is_file($filename)){
            $this->filename = $filename;
            $getimagesize = getimagesize($this->filename);
            $this->picWidth = $getimagesize[0];
            $this->picHeight = $getimagesize[1];
            $this->picMimeTypeCons = $getimagesize[2];
            if($this->allowedMime()){
                $this->picMimeType = image_type_to_mime_type($this->picMimeTypeCons);
                $this->picture = $this->imageCreate($this->picMimeTypeCons);
            }else{
                trigger_error('Mime type not allowed',E_USER_ERROR);
            }
        }else{
            trigger_error('File not found',E_USER_ERROR);
        }
    }
    
    public function imageCreate($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagecreatefromgif($this->filename);
                break;
            case IMAGETYPE_JPEG:
                return imagecreatefromjpeg($this->filename);
                break;
            case IMAGETYPE_PNG:
                return imagecreatefrompng($this->filename);
                break;
            case IMAGETYPE_BMP:
                return imagecreatefromwbmp($this->filename);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function imageCreateSlider($storePath,$width = 780,$height = 397){
        $this->storePath = $storePath;
        $divWidth = floor(($this->picWidth-$width)/2) ;
        $divHeight = floor(($this->picHeight-$height)/2);
        $this->imageWork = imagecrop($this->picture,array("x"=>$divWidth,"y"=>$divHeight,"width"=>$width,"height"=>$height));
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageAvatar($storePath,$width = 220,$height = 220){
        $this->storePath = $storePath;
        $this->imageWork = imagescale($this->picture,$width,$height);
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageCreateType($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagegif($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_JPEG:
                return imagejpeg($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_PNG:
                return imagepng($this->imageWork,$this->storePath,9,PNG_ALL_FILTERS);
                break;
            case IMAGETYPE_BMP:
                return image2wbmp($this->imageWork,$this->storePath);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function allowedMime(){
        for ($i=0; $i <count($this->allowedMimeType) ; $i++) {
            return array_key_exists($this->picMimeTypeCons,$this->allowedMimeType);
        }
    }
    public function __destruct(){
        imagedestroy($this->picture);
        if(!empty($this->imageWork)) imagedestroy($this->imageWork);
    }
}
Source Link
andrewnite
  • 355
  • 2
  • 3
  • 15

Image manipulation PHP class using PHP's Image Functions

I am working on a image manipulation class for a project to re-size uploaded slider images to 780 X 397 and uploaded avartar pictures to 220 X 220. I am wondering if this is a good way to construct it.

A question I have is what would be the best way to handle an image that is smaller then 780 X 397. Should I simply re-size it or go about it a different way?

Have have included the class code as well as the code syntax on how it is to be used

Class code

    class imageEditer{
    private $filename;
    public $picWidth;
    public $picHeight;
    public $picMimeType;
    public $picMimeTypeCons;
    private $allowedMimeType = array(IMAGETYPE_GIF=>'.gif',IMAGETYPE_JPEG=>'.jpg',IMAGETYPE_PNG=>'.png',IMAGETYPE_BMP=>'.bmp');
    public $picture;
    private $imageWork;
    private $storePath;
    
    public function __construct($filename){
        if(is_file($filename)){
            $this->filename = $filename;
            $getimagesize = getimagesize($this->filename);
            $this->picWidth = $getimagesize[0];
            $this->picHeight = $getimagesize[1];
            $this->picMimeTypeCons = $getimagesize[2];
            if($this->allowedMime()){
                $this->picMimeType = image_type_to_mime_type($this->picMimeTypeCons);
                $this->picture = $this->imageCreate($this->picMimeTypeCons);
            }else{
                trigger_error('Mime type not allowed',E_USER_ERROR);
            }
        }else{
            trigger_error('File not found',E_USER_ERROR);
        }
    }
    
    public function imageCreate($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagecreatefromgif($this->filename);
                break;
            case IMAGETYPE_JPEG:
                return imagecreatefromjpeg($this->filename);
                break;
            case IMAGETYPE_PNG:
                return imagecreatefrompng($this->filename);
                break;
            case IMAGETYPE_BMP:
                return imagecreatefromwbmp($this->filename);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function imageCreateSlider($storePath,$width = 780,$height = 397){
        $this->storePath = $storePath;
        $divWidth = floor(($this->picWidth-$width)/2) ;
        $divHeight = floor(($this->picHeight-$height)/2);
        $this->imageWork = imagecrop($this->picture,array("x"=>$divWidth,"y"=>$divHeight,"width"=>$width,"height"=>$height));
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageAvatar($storePath,$width = 220,$height = 220){
        $this->storePath = $storePath;
        $this->imageWork = imagescale($this->picture,$width,$height);
        if(strstr(basename($this->storePath),'.') != strstr(basename($this->filename),'.')){
            while ($fileEx = current($this->allowedMimeType)) {
                if(strstr(basename($this->storePath),'.') == $fileEx){
                   $this->imageCreateType(key($this->allowedMimeType));
                    return;
                }
                next($this->allowedMimeType);
            }
        }else{
            $this->imageCreateType($this->picMimeTypeCons);
        }
    }
    
    public function imageCreateType($mimeTypeCons){
        switch ($mimeTypeCons) {
            case IMAGETYPE_GIF:
                return imagegif($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_JPEG:
                return imagejpeg($this->imageWork,$this->storePath);
                break;
            case IMAGETYPE_PNG:
                return imagepng($this->imageWork,$this->storePath,9,PNG_ALL_FILTERS);
                break;
            case IMAGETYPE_BMP:
                return image2wbmp($this->imageWork,$this->storePath);
                break;
            default:
                trigger_error("Couldn't create image.",E_USER_ERROR);
                break;
        }
    }
    
    public function allowedMime(){
        for ($i=0; $i <count($this->allowedMimeType) ; $i++) {
            return array_key_exists($this->picMimeTypeCons,$this->allowedMimeType);
        }
    }
    public function __destruct(){
        imagedestroy($this->picture);
        if(!empty($this->imageWork)) imagedestroy($this->imageWork);
    }
}

Code syntax

$imageEditer->imageCreateSlider('images/slider01.png');
$imageEditer->imageAvatar('image/useravatar.png);

Any suggestion are appreciated.