I am using getimagesize() to collect image information loaded remotely.
The problem is if the remote server takes too long on the request or the image doesn't exist and I get an error timeout. What can I do to prevent this, so that if it takes 15 seconds to load automatically make the request then return some code returning null $width, $height, and $type?
if($siteImage != "None"){
list($width, $height, $type) = getimagesize($siteImage);
if(!filter_var($siteImage, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)){
die("fillDiv('checkImage','<font color=\"red\">Image is not valid URL type.</font>');");
}elseif($width != "468" || $height != "60"){
die("fillDiv('checkImage','<font color=\"red\">Incorrect size.</font>');");
}elseif($type != "1" && $type != "2" && $type != "3"){
die("fillDiv('checkImage','<font color=\"red\">Incorrect image type. (.jpg .gif .png) only</font>');");
}else{
print("fillDiv('checkImage','');");
}
}
You have two options.
1 Use getimagesize but in two lines, and hide error messages. Check for a return value
$aSize = #getimagesize($url);
if ($aSize) {
// You have a return values
list($width, $height, $type) = $aSize;
} else {
// No return values
$width = 0; // etc
}
2 User cUrl to copy the file locally. You can control the timeous better with curl, also you can check if the file exists or is taking too long to download etc. Once copied locally, use getimagesize() on the local file. It will only then fail if the file is not a genuine image. Quick online example: http://www.weberdev.com/get_example.php3?ExampleID=4009
Related
I need to create PDF's thumbnail every time it gets loaded via POST method.
Once I upload the file inside Controller, it runs getThumb function that uses Imagick to create thumbnail. The problem is that everytime I do that, this request breaks and shows this error - The "/tmp/phpY14gRo" file does not exist or is not readable..
Imagick is properly installed. I use php-7.2-apache docker image.
But if I run shell_excec script that does absolutely the same thing, it works! That eliminates all suspicions of the wrong dependency installation
Here's the function from my controller:
public function createThumb($source, $target, $size = 256, $page = 1)
{
if (file_exists($source) && !is_dir($source)): // source path must be available and not be a directory
if (mime_content_type($source) != 'application/pdf'):
return FALSE; // source is not a pdf file returns a failure
endif;
$sepa = '/'; // using '/' as file separation for nfs on linux.
$target = dirname($source) . $sepa . $target;
$size = intval($size); // only use as integer, default is 256
$page = intval($page); // only use as integer, default is 1
$page--; // default page 1, must be treated as 0 hereafter
if ($page < 0) {
$page = 0;
} // we cannot have negative values
//It breaks exactly right here
$img = new Imagick($source . "[$page]"); // [0] = first page, [1] = second page
$imH = $img->getImageHeight();
$imW = $img->getImageWidth();
if ($imH == 0) {
$imH = 1;
} // if the pdf page has no height use 1 instead
if ($imW == 0) {
$imW = 1;
} // if the pdf page has no width use 1 instead
$sizR = round($size * (min($imW, $imH) / max($imW, $imH))); // relative pixels of the shorter side
$img->setImageColorspace(255); // prevent image colors from inverting
$img->setImageBackgroundColor('white'); // set background color and flatten
$img = $img->flattenImages(); // prevents black zones on transparency in pdf
$img->setimageformat('jpeg');
if ($imH == $imW) {
$img->thumbnailimage($size, $size);
} // square page
if ($imH < $imW) {
$img->thumbnailimage($size, $sizR);
} // landscape page orientation
if ($imH > $imW) {
$img->thumbnailimage($sizR, $size);
} // portrait page orientation
if (!is_dir(dirname($target))) {
mkdir(dirname($target), 0777, true);
} // if not there make target directory
$img->writeimage($target);
$img->clear();
$img->destroy();
if (file_exists($target)) {
return $target;
} // return the path to the new file for further processing
endif;
return FALSE; // the source file was not available, or Imagick didn't create a file, so returns a failure
}
I thought that it was permission problems but found out that it's not.
Update:
If I initialize Imagick without parameters it won't throw errors and thus won't create thumbnail as it doesn't get file path. So, whenever I add file path and PHP starts searching for that file, and the error occurs. Inside the log, I noticed that the InvalidArgumentException exception was thrown by a Symfony framework.
Here's an image of the error:
After debugging I found out that Imagick was not imported into the project.
So, I just added use Imagick at the top of my Controller.
In my case i call $validator->fails() two times.
In my case, the first call $validator->fails() in my Controller action, after checking deleted the file. The second call could not find this file.
if(!empty($employeepic)) {
if ((($employeepic_type == 'image/jpg') ||($employeepic_type == 'image/jpeg') ||($employeepic_type == 'image/gif') ||
($employeepic_type == 'image/png')) && ($employeepic_size <= EMP_MAXSIZE) && ($employeepic_size > 0)){
// Move the file to the target upload folder
$target = (EMP_UPLOADPATH .$firstname.$employeepic);
if(move_uploaded_file($_FILES['employeepic']['tmp_name'],$target)){
$employee = $firstname. " " .$lastname;
}
}else{
$filetoobig =' <p class="error"> There was a problem uploading your picture. Maximum size is 30K and must be in jpg, jpeg or pjpeg format</p>';
#unlink($_FILES['employeepic']['tmp_name']);
}
}
Can anyone see why the validation of file size not working?
(EMP_MAXSIZE = 32768)
Edit: The limit size is set at 32768 but can still upload 2MB files
Edit: The code to assign Employee_pic Size:
$employeepic = mysqli_real_escape_string($dbc, trim($_FILES['employeepic']['name']));
$employeepic_type = $_FILES['employeepic']['type'];
$employeepic_size = $_FILES['employeepic']['size'];
I figured it out. I am probably not doing the "right way of coding" but it nevertheless fixed the problem. I created another variable called $employee_pic = $firstname.$employeepic; in the true section of my validation and added "employee_pic='' ;" under the false result of my validation in order not to save the picture to mysql. This was accomplished by changing the $employeepic variable for $employee_pic in my insert query.
How to verify whether the file (jpg, png, gif) is the picture in PHP?
I need function like this:
boolean isImage($url);
Or:
boolean isImage(binary_data_from_http_post);
I think second way is better, because works before saving file on disk.
I don't want to experiment with copying from random page found in google.
You can use finfo
http://de3.php.net/manual/en/function.finfo-file.php
Or get image size
http://de.php.net/getimagesize
You can use PHP's GD extension to check for a valid image:
function isImage($url) {
$buffer = file_get_contents($url);
$img = imagecreatefromstring($buffer);
return ($img !== false) ? true : false;
}
If you already have the potential image data you can use this instead:
function isImage($data) {
return (imagecreatefromstring($data) !== false) ? true : false;
}
I need to check whether a given image is a JPEG.
if ($_FILES["fname"]["error"] > 0) {
$imgData = "hyperlink/holder.jpg";
} else {
$imgData ="hyperlink/" . $_FILES["fname"]["name"];
}
// Only accept jpg images
// pjpeg is for Internet Explorer should be jpeg
if (!($_FILES["fname"]["type"] == "image/pjpeg") ) {
print "I only accept jpg files!";
exit(0);
}
When it goes to first statement in the first if statement it always gives I only accept jpg files!
How can I fix it?
Try the exif_imagetype image function.
Example:
if(exif_imagetype($filepath) != IMAGETYPE_JPEG){
echo 'Not a JPEG image';
}
PHP has such good image-type support, i wonder why you are restricting your app. In just a couple lines of code you can deal with any input format and convert to jpeg, if that is a requirement...
$im = imagecreatefrompng(input_filename)
imagejpeg($im, output_filename);
I believe the following works:
Also note that:
(exif_imagetype($ImagePathAndName) == IMAGETYPE_JPEG)
only reads the first few bytes looking for an image header so isn't really good enough to confirm if an image is corrupt.
Below I have it in a logical “and” statement i.e. both of these tests must be passed in order for the image to qualify as being valid and non-corrupt etc:
if ((exif_imagetype($ImagePathAndName) == IMAGETYPE_JPEG) && (imagecreatefromjpeg( $ImagePathAndName ) !== false ))
{
echo 'The picture is a valid jpg<br>';
}
Note: You need to place this line of code at the top of the php code in order to avoid seeing the warning messages from imagecreatefromjpeg( $ImagePathAndName ) when it encounters a fake/corrupt image file.
ini_set(‘gd.jpeg_ignore_warning’, 1);
Why don't you try creating an array of exceptions (the files you want the user to be able to upload).
// Hyperlink for your website
$hyperlink = "http://www.yourwebsitehere.com";
if($_FILES['fname']['error'] > 0)
{
$image= $hyperlink . "/holder.jpg";
}
else
{
$image = $hyperlink . "/" . $_FILES['fname']['name'];
}
// Only accept files of jpeg format
$exceptions = array("image/jpg", "image/jpeg", "image/pjpeg");
foreach($exceptions as $value)
{
if($_FILES['fname']['type'] != $value)
{
echo "I only accept jpeg images!";
break; // Or exit();
}
}
When using $_FILES, you are relying on informations sent by the client, which is not the best thing to do (you've seen it's not always the same, and, if I remember correctly, $_FILES['...']['type'] can be faked).
If you are using PHP >= 5.3 (or can install PECL packages), maybe you can give a look to the extension Fileinfo. If you are using an older version, what about mime_content_type?
And, as said by Scott, why allow only jpeg?
Looking about the code better : when you are in the first case (error > 0), you are assigning a default file to $imgData? Why the spaces around "hyperlink"?
And why do you always use to check the content-type, even if there was an error a couple of lines before?
To finish, did you have a look at the manual (Handling file uploads)?
Check the mime (Multipurpose Internet Mail Extensions) type of file with this code. And verify your desired type. You can also detect png,gif with this code.
if($_FILES["fname"]["type"] == "image/jpeg")
{
echo "File type is JPEG";
}
I am looking for a way to take a user uploaded image that is currently put in a temporary location ex: /tmp/jkhjkh78 and create a php image from it, autodetecting the format.
Is there a more clever way to do this than a bunch of try/catching with imagefromjpeg, imagefrompng, etc?
This is one of the functions of getimagesize. They probably should have called it "getimageinfo", but that's PHP for you.
//Image Processing
$cover = $_FILES['cover']['name'];
$cover_tmp_name = $_FILES['cover']['tmp_name'];
$cover_img_path = '/images/';
$type = exif_imagetype($cover_tmp_name);
if ($type == (IMAGETYPE_PNG || IMAGETYPE_JPEG || IMAGETYPE_GIF || IMAGETYPE_BMP)) {
$cover_pre_name = md5($cover); //Just to make a image name random and cool :D
/**
* #description : possible exif_imagetype() return values in $type
* 1 - gif image
* 2 - jpg image
* 3 - png image
* 6 - bmp image
*/
switch ($type) { #There are more type you can choose. Take a look in php manual -> http://www.php.net/manual/en/function.exif-imagetype.php
case '1' :
$cover_format = 'gif';
break;
case '2' :
$cover_format = 'jpg';
break;
case '3' :
$cover_format = 'png';
break;
case '6' :
$cover_format = 'bmp';
break;
default :
die('There is an error processing the image -> please try again with a new image');
break;
}
$cover_name = $cover_pre_name . '.' . $cover_format;
//Checks whether the uploaded file exist or not
if (file_exists($cover_img_path . $cover_name)) {
$extra = 1;
while (file_exists($cover_img_path . $cover_name)) {
$cover_name = md5($cover) . $extra . '.' . $cover_format;
$extra++;
}
}
//Image Processing Ends
this will make image name look cool and unique
Use exif_imagetype() if it's available ..:
http://www.php.net/manual/en/function.exif-imagetype.php
I'm pretty sure exif functions are available by default (i.e. you have to specifically exclude them rather than specifically include them) when you install php
You could try finfo_file(), apparently an improved version of mime_content_type().
Edit: OK, getimagesize() is better..
You can call a system command (if you're under linux/unix), file if you like:
kender#eira:~$ file a
a: JPEG image data, EXIF standard 2.2
This will help you to know the Extension as well as result based on condition
$image_file = 'http://foo.com/images.gif';
$extension = substr($image_file, -4); if($extension == ".jpg"){ echo 'Its a JPG Image.'; } else { echo 'Its not a JPG Image.'; }
People are recommending using getimagesize() but the documentation reads:
Caution This function expects filename to be a valid image file. If a
non-image file is supplied, it may be incorrectly detected as an image
and the function will return successfully, but the array may contain
nonsensical values.
Do not use getimagesize() to check that a given file is a valid image.
Use a purpose-built solution such as the Fileinfo extension instead.
The relevant function in the Fileinfo extension is finfo_file():
string finfo_file ( resource $finfo , string $file_name = NULL
[, int $options = FILEINFO_NONE [, resource $context = NULL ]] )
Returns a textual description of the contents of the file_name
argument, or FALSE if an error occurred.
Example return values given are: text/html, image/gif, application/vnd.ms-excel
However, comments on the official documentation page warn that this shouldn't be relied on for validation either.