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.
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;
}
}
}