Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a method to validate an image from a URL. The method will accept an array of image URL. The image should be validated by its type, size and dimensions. Right now, I'm able to implement it but I'm having a problem on how long it takes to finish the validation.

enter image description here

Is there any alternative on my current approach that might speed up the validation process?

public function validate_attachment($attachment){
    $image = "";
    if(strpos($attachment,",") !== false){
      $image = explode(",", $attachment);
    }else{
      $image = array($attachment);
    }

    foreach ($image as $url) {

      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_exec($ch);

      switch (curl_getinfo($ch, CURLINFO_CONTENT_TYPE)) { //validate file type

        case 'image/png' :
        case 'image/jpg' :
        case 'image/jpeg':
        case 'image/gif' :
          echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE) ."<br />";
          $type = true;
          $filesize =  curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD)/1024 ."<br/>"; // validate image size
          echo $filesize;
          if($filesize <= 70){
            echo $filesize;
            list($width, $height) = getimagesize(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); // validate image dimensions
            if($width <= 800 && $height <= 600){
              echo "width: " . $width . "<br />";
              echo "height: " .  $height ."<br/>";
            }else{
              echo "Image too large";
            }
            curl_close($ch);

          }else{
            echo "file size should be below 70kb";
          }
          break;
        default:
          echo "Invalid file type.";
          break;
      }
    }
  }
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.