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);
      $image = array($attachment);

    foreach ($image as $url) {

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

      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/>";
              echo "Image too large";

            echo "file size should be below 70kb";
          echo "Invalid file type.";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.