I would like to know if there is a way to change an uploaded files dimentions to a specified dimention and make the size of the file adhere to a specified size, I have a person uploading huge files after being told to make them smaller, this guy refuses to listen to me can I force the files to shrink and adhere to my rules?

Recommended Answers

All 3 Replies

Hi,

Here is an old image manipulator I wrote early last year . I have not tested it since then, but I still believe it will work..

<?  
## this image manipulator class was written by veedeoo or poorboy 2012
## feel free to modify, extend this clas as you wish.
## does not have any warranty of any kind

class ReduceImage{
    private $imageLoc = null;
    private $outImageLoc = null;
    private $imageResizePoint = null;


public function __construct($imageLoc,$outImageLoc,$imageQuality){
    $this->imgLoc = $imageLoc;
    $this->outLoc = $outImageLoc;
    $this->quality = $imageQuality;
}

protected function getImageInfo(){
 list($this->x,$this->y) = getimagesize($this->imgLoc);

 return array($this->x, $this->y);

}
public function createImage(){
    $this->imageX_Y = self::getImageInfo();
    $this->newImage = imagecreatetruecolor($this->imageX_Y[0],$this->imageX_Y[1]);

    ## in production server use code below
    //$this->newImage = @imagecreatetruecolor($imageX_Y[0],$imageX_Y[1])

  //$this->newX_Y = self::getImageInfo();
  $this->newfile = imagecreatefromjpeg($this->imgLoc);
  imagecopyresampled($this->newImage, $this->newfile, 0, 0, 0, 0, $this->imageX_Y[0], $this->imageX_Y[1], $this->imageX_Y[0], $this->imageX_Y[1]);


 imagejpeg($this->newImage,$this->outLoc,$this->quality);
 ## in production server use code below
 //@imagejpeg($this->newImage,$this->outLoc,$this->imageResize);

// return array($this->newImage,$this->outLoc,$this->newfile);

return $this->outLoc;
imagedestroy($this->newImage);
imagedestroy($this->newfile);

}




}

To use the class above, just provide the required parameters..

  ## quality of 50 is acceptable, but you may want to experiment from 60.
$quality = 60; 
$reducethis = new ReduceImage("images/sample.jpg","images/output1.jpg",$quality);
$image = $reducethis->createImage();

echo '<img src="'.$image.'">';

I hope this helps. If not, there should be someone else here that is a lot knowledgeable than me.. I am pretty sure of that..

The above is the quality scaler or reducer that will help you reduce the file size. Below is another class I wrote from early last year. This has already been posted here at daniweb, but I can't find it.

Class below will properly scale any images to your desired size

<?php
## filename Scaler.class.php
## simple demonstration on how the singleton pattern can be use in OO PHP..
## written by veedeoo or poorboy 2012 , from daniweb.com ,phpclasses.org, tutpages.com, and yahoo php expert community.
## this script will be available at http://www.phpclasses.org/ .

class Scaler{
   ## Singleton Pattern begins
   public static $instance = NULL;
   private function __construct(){
                ## if there will be another class needed by this class, you can create an instance here, else just leave this empty
            }
   public static function getInstance() {
                if(!isset(self::$instance)) {

                ## we create an instance of the class Scaler if there is no instance running
                  self::$instance = new Scaler();
                }
                return self::$instance;
              } 
    private function  __clone() {
      ## don't worry about this, just to make sure there is no clone going in the background.
    }

    ## end of singleton pattern
    ## class Scaler methods begins  
    public function createThumb($filename, $maxW, $maxH, $image_path, $thumb_path, $outname) {

    $ext = substr($filename, strrpos($filename, ".") + 1) ;
    switch (strtolower($ext)) {
        case "jpg":
        case "jpeg":
            $img = imagecreatefromjpeg($image_path.$filename) ;
            $size = self::ResizeMath($maxW, $maxH, $img) ;
            $size[0] = $maxW ;
            $size[1] = $maxH ;
            $img2 = imagecreatetruecolor($size[0], $size[1]) ;
            imagecopyresized($img2, $img, 0, 0, 0, 0, $size[0], $size[1], $size[2], $size[3]) ;
            imagejpeg($img2, $thumb_path.$outname) ;
            return true ;
            break ;
        case "png":
            $img = imagecreatefrompng($image_path.$filename) ;
            $size = self::ResizeMath($maxW, $maxH, $img) ;
            $size[0] = $maxW ;
            $size[1] = $maxH ;
            $img2 = imagecreatetruecolor($size[0], $size[1]) ;
            imagecopyresampled($img2, $img, 0, 0, 0, 0, $size[0], $size[1], $size[2], $size[3]) ;
            imagepng($img2, $thumb_path.$outname) ;
            return true ;
            break ;
        case "gif":
            $img = imagecreatefromgif($image_path.$filename) ;
            $size = self::ResizeMath($maxW, $maxH, $img) ;
            $size[0] = $maxW ;
            $size[1] = $maxH ;
            $img2 = imagecreatetruecolor($size[0], $size[1]) ;
            imagecopyresampled($img2, $img, 0, 0, 0, 0, $size[0], $size[1], $size[2], $size[3]) ;
            imagegif($img2, $thumb_path.$outname) ;
            return true ;
            break ;
        default:
            return false ;
            break ;
    }
}
    private function ResizeMath($maxW, $maxH, $img) {
        $imageW = imagesx($img) ;
        $imageH = imagesy($img) ;
        if ($imageW <= $maxW && $imageH <= $maxH) {
            $thumbW = $imageW ;
            $thumbH = $imageH ;
    }
        else {
                if ($imageW > $imageH) {
                 ## this is wide image
                    $qW = $maxW / $imageW;
                    $thumbW = $imageW * $qW ;
                    //$thumbH = $maxW / $imageW * $imageH ;
                    $thumbH = $imageH * $qW;
                }
                elseif($imageH > $imageW){
                    ## we got tall image
                    $qH = $maxH / $imageH;
                    $thumbH = $imageH * $qH;
                }
                else {
                    $thumbH = $maxH ;
                    $thumbW = $maxW ;
                }
            }
    $thumbW = round($thumbW) ;
    $thumbH = round($thumbH) ;
    $thesize = array($thumbW, $thumbH, $imageW, $imageH) ;
    return $thesize ;
    }   
}

thank you very much I will try thoseand let you know if they work or not

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.