php ensure file_get_contents images not corrput -
i have php script use file_get_contents download remote images server, sometime corrupted
$contents = file_get_contents($url); list($width,$height) = getimagesizefromstring( $contents ); if($width <= 300 ){ $contents = base64_decode('isdfafasfgoaaaansuheugaaaaeaaaabaqmaaaal21bkaaaaa1bmveuaaacnej3aaaaaaxrstlmaqobyzgaaaapjrefucndjyaaaaaiaaeihvdmaaaaasuvork5cyii='); $downloadlog->log( "ignore file width below 300 : " . $url ); } $aws->writetoaws( $target_path , $contents ); $contents = null;
are there ways ensure images downloaded correctly without corruption or way validate images against remote url?
you can validate using getimagesize
note: formats may contain no image or may contain multiple images. in these cases, getimagesize() might not able determine image size. getimagesize() return 0 width , height in these cases.
on failure, false returned.
edited:
for example
<?php $array = getimagesize('https://dl.dropboxusercontent.com/u/22492671/not_an_image.jpg'); if( $array ){ echo '<pre>'; print_r($array); echo '</pre>'; } else { echo 'image broken'; } ?>
getimagesize()
return false when image broken.
Comments
Post a Comment