...

View Full Version : check image



Chris-2k
08-18-2012, 12:29 AM
hi

i have a func to check if an image is valid, works perfect for upload from pc an url:
function checkitsaValidImage($files, array $aConfig) {
$imageInfo = getimagesize($files);
$sResult = '';

if(!$imageInfo) {
trigger_error('Error not an image.');
}

if(!empty($imageInfo) && !in_array($imageInfo['mime'], $aConfig['allowed_mime_types'])) {
trigger_error('Error invalid file.');
} else if($imageInfo[0] > $aConfig['max_width'] || $imageInfo[1] > $aConfig['max_height']) {
trigger_error('Error image size too big.');
} else if(filesize($files) > configMaxSize($aConfig['max_filesize'])) {
trigger_error('Error file size too big.');
}
else
{
$sResult = image_type_to_extension($imageInfo[2], true);
}
return $sResult;
}

only issue is the filesize(); func WILL not check URLs/remote, erm i'm wondering can a globalFS(); func that can get remote/URLs AND pc uploads and then call it in place of filesize();?

Lamped
08-18-2012, 12:33 AM
filesize() can check some remote URLs but your ISP may have disabled fopen wrappers.

If you need to check URLs, cURL may be your best bet (perhaps custom method of HEAD will still return the content length header) but that's going to be flaky too as some pages just won't tell you.

Chris-2k
08-18-2012, 01:03 AM
I can do it via curl, hmm

can i do;



globalFS()
{
if(filesize($files)) {
}
else
{
// try curl
}
}

an then call it instead of filesizze()?

byrondallas
08-18-2012, 12:46 PM
This is what I use to check size of remote images and it seems to work pretty good:


$string = file_get_contents($url);
$length = strlen($string);

Chris-2k
08-18-2012, 09:33 PM
hi guys,

can u tell me where nim goin wrong[;

function handleSize($files)
{
if(filesize($files))
{
// It's a local file.
} else
{
// It's remote file.
$ch = curl_init($files); // Get the filesize using curl
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false)
{
echo "<p align='center' classs='select_text'>Unable to upload your image as you didn't provide a valid URL</p>";
exit;
}

$remoteSize = 'unknown';
$status = 'unknown';

if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches))
{
$status = (int)$matches[1];
}

if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
$remoteSize = (int)$matches[1];
}
}
}

im gettin
Unable to upload your image as you didn't provide a valid URL

AndrewGSW
08-18-2012, 09:44 PM
If you have spaces in the filename:


$url = str_replace(' ', '%20', $files);
$ch = curl_init($url); // Get the filesize using curl

Chris-2k
08-18-2012, 09:58 PM
Sorry Andrew, I dont follow................

AndrewGSW
08-19-2012, 01:21 AM
Sorry Andrew, I dont follow................


Spaces in the URL need to be replaced with a %20.

mentioned in the user contributed notes of the docs (http://www.php.net/manual/en/function.curl-init.php).

Chris-2k
08-19-2012, 02:29 PM
oh yea thx m8, some people misread my question....

I am not looking HOW TO do this, rather A WAY to do it... I want to use curl, all funcs i've written til now are fail, my final attempt:


function handleSize($files)
{
if($files)
{
// It's a local file.
$size = filesize($files);
} else
{
// It's remote file.
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);

$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

curl_close($ch);
}
return $size;
}

now for my chck func can i do:



if(handleSize($files) > $size)
{
// error
}

Chris-2k
08-19-2012, 05:27 PM
anyone can help?

AndrewGSW
08-19-2012, 06:28 PM
You seem to have introduced the variable $url:


// It's remote file.
$ch = curl_init($url);

If you are adopting my code sample then you need both lines.

I am not sure about your distinction between HOW TO and A WAY to, but the documentation is quite clear in insisting that spaces should be removed from the url. But perhaps your url doesn't have any spaces.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum