2
\$\begingroup\$

I have working code that uploads and resizes images. What im trying to figure out is how I can direct the browser to another page or view kind of the way facebook does, but still call the resize function in the background. Right now, the code waits to complete the resize before doing anything else.

The Call to the upload and resize functions on the upload page :

if ($result=$imageHandler->upload($files)) 
{
 //call image resize, call resize
 $success= new view($result);
 $imageHandler->imgResize($result);
}

And this is the FileHandler class that uploads and resize the image:

 <?php
/*
 > Class fileHandler
 > Handle existing files and existing folders.
 */
 class ImageHandler {
 protected $required;
 protected $imagePath;
 protected $allowedfiles;
 protected $default;
 //constructor 
 public function __construct()
 {
 require_once 'UploadException.php';
 $this->allowedfiles=array( 'image/gif', 'image/jpg', 'image/jpeg', 'image/png' );
 $this->required=array('800', '1024','1200','1280','1400','1440','1600','1680','1920','2400','2500','2600','2880');
 //use absolute path with defined constant here
 $this->default='images/';
 }
//UPLOAD FUNCTION CALLED FIRST, RETURNS UPLOADED IMAGE LOCATION . Includes Exception Handling 
 public function upload($files)
 { 
 //Check contents of Files array. Class should not interact directly with $_FILES or $_POST . 
 //Check array is not empty and file type is allowed. 
 if (!empty($files) && in_array($files['type'], $this->allowedfiles) ) 
 {
 $temp=$files['tmp_name'];
 $destination=$this->default.$files['name'];
 //if all good, check file size less than 20MB or something?
 //proceed with upload. Include error handling in this if statement
 //if file upload unsuccessful, do error handling. else return new file location
 if (move_uploaded_file($temp, $destination)) {
 echo '<hr> file upload worked';
 return $destination;
 //exit('all good'.print_r($files));
 } else{
 //call upload error handler. Get error Msgs.
 //throw new exception and pass it error code returned by upload! 
 $error= new UploadException($files['error']);
 $error->getMessage();
 }
 } else{
 //handle failure with exception handler
 $error=new UploadException($files['error']);
 echo $error;
 exit('<br><br>execution stopped');
 }
 }
 //takes image path returned by upload function
 public function imgResize($image)
 { 
 $required=$this->required;
 $details=getimagesize($image);
 $count=count($required);
 $imgName=substr($image, strrpos($image,'/')+1 );
 $imgType=$details['mime'];
 $width=$details[0];
 $height=$details[1];
 $imgRatio=$details[0]/$details[1];
 $GDSrcImg=$this->createImage($image );
 for ( $i = 0; $i < $count ; $i++ ) 
 { 
 //if the destination folder doesnot exist, create it. 
 if(!file_exists('images/'.$required[$i]))
 {
 mkdir( 'images/'.$required[$i], 0764, true );
 chmod( 'images/'.$required[$i], 0764 );
 } else {
 $userDestImage = 'images/'. $required[$i] . '/' . $imgName;
 $newWidth = intVal( $required[$i] );
 $newHeight = intVal( $newWidth / $imgRatio ); //maintain aspect ratio
 $dest = imagecreatetruecolor( $newWidth, $newHeight );
 imagecopyresampled( $dest, $GDSrcImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height );
 $finalImg= $this->finalImg($imgType, $dest, $userDestImage ); 
 } 
 }
 return $finalImg;
 }
 public function createImage( $img ) 
 {
 $size = getimagesize( $img );
 $typeImg = $size['mime'];
 switch ( $typeImg ) {
 case 'image/png':
 $newImage = imagecreatefrompng( $img );
 return $newImage;
 break;
 case 'image/jpeg':
 $newImage = imagecreatefromjpeg( $img );
 return $newImage;
 break;
 case 'image/gif' :
 $newImage = imagecreatefromgif( $img );
 return $newImage;
 break;
 }
 }
 public function finalImg($imgType,$dest,$userDestImage)
 {
 switch ( $imgType ) {
 case 'image/png':
 $final = imagepng( $dest, $userDestImage );
 //return $final;
 break;
 case 'image/jpeg':
 $final = imagejpeg( $dest, $userDestImage );
 //return $final;
 break;
 case 'image/gif' :
 $final = imagegif( $dest, $userDestImage );
 //return $final;
 break;
 }
 return $final;
 }
}
asked Aug 2, 2015 at 16:00
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I don't have an answer for your question, but I do have a couple of points about the code you do have.

Security

type is user controlled, and shouldn't be trusted. With your current code, it would be possible to upload PHP files and thus gain code execution.

What you should do is check the file extension as well as the actual file type. The functions that are generally recommended for this are pathinfo and finfo_file respectively.

Misc

  • echoing or exiting in a class is generally not recommended, because it makes it hard to reuse.
  • you have a lot of unnecessary newlines, which makes the code harder to read.
  • if a variable is only ever used once, you can just remove it (see eg $error).
  • use more spaces (eg around = or .).
  • most of your inline comments are not necessary, as they just describe what the code does.
  • your indentations and placing of brackets is not internally consistent, which reduces readability.
answered Aug 2, 2015 at 21:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.