in my php page I have <input type='file' name='aimage[]' id='aimage' />
and a snippet code to get this file as :
foreach($_FILES["aimage"]["tmp_name"] as $file)
{
/*** get the image info. ***/
if(is_uploaded_file($file) && getimagesize($file) != false){
$size = getimagesize($file);
/*** assign our variables ***/
$type = $size['mime'];
$imgfp = fopen($file, 'rb');
$size = $size[3];
// $name = $file['name'];
$maxsize = 99999999;
/*** check the file is less than the maximum file size ***/
//if($_FILES['aimage']['size'] < $maxsize )
// {
/*** connect to db ***/
$formvars['file'] = fread($imgfp, getimagesize($file));
Although using foreach, I got error as Warning: fread() expects parameter 2 to be long, array given in. what's cause of this error? How can I check only images uploaded to site and other files detected by code?
The error is caused by the second parameter of fread() : getimagesize($file)) which returns an array and not a long. getimagesize returns an array(width, height)
Maybe you want to use filesize($file) instead
From the getimagesize documentation:
Returns an array with up to 7 elements. Not all image types will
include the channels and bits elements.
Index 0 and 1 contains respectively the width and the height of the
image.
Note: Some formats may contain no image or may contain multiple
images. In these cases, getimagesize() might not be able to properly
determine the image size. getimagesize() will return zero for width
and height in these cases. Index 2 is one of the IMAGETYPE_XXX
constants indicating the type of the image.
Index 3 is a text string with the correct height="yyy" width="xxx"
string that can be used directly in an IMG tag.
mime is the correspondant MIME type of the image. This information can
be used to deliver images with the correct HTTP Content-type header:
Example #1 getimagesize() and MIME types
Related
i have this code:
$album_name = $row['album'];
if(!file_exists("cdcovers/$album_name.jpg") && filesize($album_name) > 5){
//gravar as imagens
$imageString = file_get_contents(LastFMArtwork::getArtwork($row['artist'], $row['album'], true, "large"));
$save = file_put_contents('/home/link/public_html/cdcovers/'.$row['album'].'.jpg',$imageString);
}
but i gives an error(Warning: filesize(): stat failed for...) if the image is not there,
my ideia was if the file exists and is bigger then 5kb then do nothing if it is 4kb or below save image, even if a file exists with 0kb.
Base on what I understand you want to create an image if it does not exist or update the image if it is smaller than 5kb.
In short, filesize will yield the warning if you give it a file that does not exists but there are a few more issues with the original code:
The condition block is wrong. The function filesize returns the size in bytes and 5kb is ~5,000 bytes.
The wrong comparison operator is also been use. The less than operator < should be used instead greater than operator >.
The code attempts to check the size of a file when it does not exist.
You are referencing different file paths in the condition block.
I modified the sample code to store the absolute path to the image in a variable and reference it where needed, fixed the bytes comparison and updated the condition block requirements to prevent the filesize warning.
Please review the updated code below:
$albumCoverImg = '/home/link/public_html/cdcovers/' . $row['album'] . '.jpg';
$minSize = 5 * 1000; // 5KB
$fileExist = is_file($albumCoverImg);
if (!$fileExist || ($fileExist && filesize($albumCoverImg) < $minSize)) {
//gravar as imagens
$imageString = file_get_contents(LastFMArtwork::getArtwork($row['artist'], $row['album'], true, "large"));
$save = file_put_contents($albumCoverImg, $imageString);
}
You have to check if the file exists before to check its size.
If "cdcovers/$album_name.jpg" and $album_name are different files, you also have to check the second.
if (!file_exists("cdcovers/$album_name.jpg") &&
file_exists($album_name) &&
filesize($album_name) > 5) {
If the files are the same, it's the error (missing folder and file extension).
Also, you save the file with an absolute path : '/home/link/public_html/cdcovers/'.$row['album'].'.jpg'. It could differ from "cdcovers/$album_name.jpg" (depending of the current folder's script execution). You may use the absolute path, of __dir__ constant to be relative to your current folder.
You may use variables to store filename and limit errors by writing them multiple times :
$filename = '/home/link/public_html/cdcovers/'.$row['album'].'.jpg' ;
if (!file_exists($filename) ||
(file_exists($filename) && filesize($filename) < 5000)) {
file_put_contents($filename, ...);
}
Please also note that filesize() returns the size in bytes, not in Kb.
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
I have some images on my system that were saved without a file extension, they appear in the format of "FILENAME." (Note the period)
I am trying to use getimagesize(); but it is erroring out telling me that "Filename cannot be empty"
Here is a sample of the code I am using
$contents = file_get_contents($localFile);
$imageData = getimagesize($contents);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);
Look at the documentation: http://php.net/getimagesize
getimagesize() is expecting a filename as first parameter, not the contents of the image file.
Try:
$imageData = getimagesize($localFile);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);
getimagesize expects a file name as the first argument
I've seen this somewhere, but can't seem to remember. How can I use getimagesize() to return the file type?
$url = (POST_IMAGESELECTOR);
$path_parts = pathinfo($url);
if ($path_parts !='') {
$extension = $path_parts['extension'];
} else {
$extension = getimagesize($url);
}
Given the documentation, getimagesize return an array of 7 values.
The one you can use are:
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
And / Or:
mime is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header
From the manual:
getimagesize Returns an array with 7 elements.
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of
the image.
Take a look at the manual for getimagesize(). Its return value is an array with the third element indicating the image's type.
Just read the documentation:
Returns an array with 7 elements.
...
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
...
Try this:
$image = getimagesize('/path/to/image.gif') ;
$imagetype = $image['mime'];
echo $imagetype . "<br />";
print_r($image);
I'm trying to show all images within a specified directory.
The following code lists all allowed file names:
function getDirectoryList()
{
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($this->currentDIR);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// Make sure we get allowed images types
if ($this->allowedFileType($file,$this->allowedImageTypes) )
{
$results[] = $file;
}
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
(...)
$images = getDirectoryList();
foreach($images as $img) {
echo "<li>".$img."</li>";
}
How can I get file size and MIME type?
I read that mime_content_typeis deprecated and I should use finfo_file istead. But I've not been very successfull with this.
Do I have to use /usr/share/misc/magic to get file information? Can't I use GD library?
I've looked at many examples, but they are old and don't work that well.
Any help appreciated.
to get the size and mime type of image its simple,
use function : getimagesize
uses like :
list($width, $height, $type, $attr) = getimagesize("img/myimg.jpg");
Returns an array with 7 elements.
Index 0 and 1 contains respectively the width and the height of the image.
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
using filesize give the size in bytes
To expand on Haim Evgi's post, use getimagesize() to retrieve the dimensions and the image type in an array. Then, use image_type_to_mime_type() on the image type code to retrieve the MIME:
list ($fileWidth, $fileHeight, $fileType) = getimagesize($filename);
$fileMimeType = image_type_to_mime_type($fileType);