i know there is a lot of question in this topic, i tried almost every one procedure but my issue is still same...
Problem is
i am trying to upload an image file and want to get image height and image width, i search a lot and find that getimagesize will do it, but when i try this code
$file=$_FILES['myfile'];
list($width, $height) = getimagesize($file);
$file_width=$width;
$file_height=$height;
then this error is shown..
Warning: getimagesize() expects parameter 1 to be string, array given
After this error i search more about that function and the i try this code....
list($width, $height) = getimagesize($_FILES['myfile']['tmp_name']);
$file_width=$width;
$file_height=$height;
after executing the code nothing would happen...
** Update** Actually i am trying to get the image height and width and save the information in database. So i want that when user upload picture then before saving the information i get the height and width of image file and the other thing is that image source would be any this like from usp or memory card, so don't say me that to pass the full path as parameter in getimagesize(). Kindly provide me proper solution, if there is any other way to get the file height and width then refer me to it.
first you need to upload file then pass the uploaded location
getimagesize("location_of_file/".$file_name);
OR
getimagesize($path['path'].'/'.$_POST['name']);
OR
$file_name=$_FILES['fileToUpload']['name'];
getimagesize($target_dir.'/'.$file_name);
OR
$file_tmp=$_FILES['fileToUpload']['tmp_name'];
list($width, $height) = getimagesize($file_tmp);
$file_width=$width;
$file_height=$height;
Check this out. After you have completed the file upload, everything should be fine.
$temporary_name = $file['painting_picture']['tmp_name'];
if (is_uploaded_file($temporary_name)) {
list($width, $height, $type, $attr) = getimagesize($temporary_name);
}
You do not do anything with your data. Try to echo it:
if (isset($_FILES['myfile']['tmp_name'])) {
list($file_width, $file_height) = getimagesize($_FILES['myfile']['tmp_name']);
echo "The file dimensions is: width ".$file_width."px, height: ". $file_height."px";
}
Related
I'm using the Intervention Image package for manipulating the uploaded image before saving it to the server. I have successfully set the dimension of the image, and it's working fine, but I want to fix the image size, i.e., the image generated should be less than equals to 30KB and should have a resolution of 300 dpi only.
Here is the code, I'm using to resize the uploaded image:
$file = $post->file('profile_image');
$filename = Carbon::now()->timestamp.'_'.$file->getClientOriginalName();
\Image::make($file->getRealPath())->resize(160, 160)->save('uploads/profile/'.$filename);
If my concern is feasible with the Intervention Image package, please give me a solution.
Thanks in advance.
you can try somthing like this
$size = 160;
$img = \Image::make($file->getRealPath())->resize($size, $size)->save('uploads/profile/'.$filename);
while($img->filesize() < "someAmount"){
$size = (int)$size - 20; // adjust based on your need
$img = \Image::make($file->getRealPath())->resize($size, $size)->save('uploads/profile/'.$filename);
}
NOTE this is not correct answer but it is idea to solve this problem
Short version
When I try to run file_get_contents() with this link, 'http://s1.reutersmedia.net/resources/r/?m=02&d=20131205&t=2&i=817503382&w=&fh=&fw=&ll=700&pl=378&r=CBRE9B401AG00', it returns: "illegal: d - msg". Why is it that file_get_contents() works on most image link but not this one, and how can I make it work?
Details
Part of my webapp's functionality is to parse external html files for images, then allow the user to select a desired image, and automatically save a reduced-size version of the image to my server. My code works for 99% of cases, but for the remaining 1% I am unable to successfully get the image file onto my server in order to re-size it. The cases that don't work seem to all involve html elements with 'src' attributes that look like this:
http://s1.reutersmedia.net/resources/r/?m=02&d=20131205&t=2&i=817503382&w=&fh=&fw=&ll=580&pl=378&r=CBRE9B401AG00
as opposed to a more standard image path such as this:
http://www.wired.com/images_blogs/wiredscience/2013/12/keyes-wd.jpg
Below is the code that I use in order to get and save the external image once the user has selected it, where the variable $newFileName is equal to an img path string such as the ones pasted above:
$contentOrFalseOnFailure = file_get_contents($newFileName);
$byteCountOrFalseOnFailure = file_put_contents($filenameOut, $contentOrFalseOnFailure);
$fileName = basename($newFileName);
$fileTmpLoc = $filenameOut;
$fileSize = $byteCountOrFalseOnFailure;
$fileExt = pathinfo($fileTmpLoc, PATHINFO_EXTENSION);
list($width, $height) = getimagesize($fileTmpLoc);
if($width < 10 || $height < 10){
header("location: ../message.php?msg=ERROR: That image has no dimensions");
exit();
}
When the src is non-standard, the script doesn't make it beyond this point, ie i get the "That image has no dimestions" error. What can I do to save save these non-standard images?
If you're interested in JUST the image dimensions and nothing else about it, you could go with GD's imagecreatefromstring() without needing a temp on-disk file:
$img = file_get_contents($url);
$gd = imagecreatefromstring($img);
$width = imagesx($gd);
$height = imagesy($gd);
This has the downside of having to decompress the image into memory, however. You'd have to hope that the remote server doesn't sent over a ludicriously dimensioned image that doesn't exceed the PHP memory_limit upon decompression.
Ignore the URL, look at the Content-Type header in the response.
When I use the GD image library the orientation EXIF data is missing, is it possible to get the data straight from the raw data string before I use imagecreatefromstring() to make the jpg file? The orientation data is there because when I look at the email in gmail in chrome it shows the proper orientation, but as soon as I download the image that data is lost. I also tried using exif_thumbnail to see if I could figure out the orientation from that but the thumbnail is missing too.
If I download the attachment through chrome, windows image viewer displays it as landscape, but if I upload it to flickr it is displayed portrait, what is flickr using to determine orientation?
EDIT: solved it
createimagefromstring strips out a ton of meta data from the image file, but using fwrite creates the file verbatim from the data string, so I did this
$filename = 'pic.jpg';
$r = fopen($filename,'x');
fwrite($r,$raw_data); //$raw_data is the data string of the image
fclose($r);
and voila all the exif data was there! Stupid GD library! >:(
If you're really using the GD library like you say you are, you can leverage getimagesize() to check for the image's dimensions. If the width is greater than the height of the image, you can safely assume that it was taken in landscape mode.
$params = getimagesize($image);
$width = $params[0];
$height = $params[1];
if ($width > $height) {
$mode = "landscape";
} else if ($width < $height) {
$mode = "portrait";
}
hi guys i am using <input type='file' name='filett' size='filett'> and move the file into the temporary location. Just i wants to know how to get the image size using php . i am using $rect = thegetimagesize("img/flag.jpg"); but if i echo the variable $rect it shows the error
Use imagesx(), imagesy(), or getimagesize()
You mean the filesize or dimensions?
Dimensions
getimagesize() will get a bunch of info about an image. Easiest way to get width and height is to assign it to list($width, $height) language construct.
Filesize
filesize() will get the size of bytes in the file. Divide by 1024 to get kilobytes and so forth.
I'm using imagecache_create_path() and getimagesize() to get the path of a imagecache-generated image and its dimensions. However, if it's the first time we access the page that image doesn't exist yet and imagecache_create_path doesn't generate it either.
Here's the code:
// we get the image path from a preset (always return the path even if the file doesn't exist)
$small_image_path = imagecache_create_path('gallery_image_small', $image["filepath"]);
// I get the image dimensions (only if the file exists already)
$data_small = list($width, $height, $type, $image_attributes) = #getimagesize($small_image_path);
Is there any API method to get the path AND generate the file? In other words, can I generate the image (using a preset) from PHP without showing it in the browser?
Thank you in advance
Check the imagecache_build_derivative() function and its usage in imagecache.module. For your case, it should work roughly like so:
$presetname = 'gallery_image_small';
$preset = imagecache_preset_by_name($presetname);
$src = $image["filepath"];
$dst = imagecache_create_path($presetname, $src);
// Ensure existing derivative or try to create it on the fly
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
// Do what you need to do with the image
}
(NOTE: untested code, beware of typos and other errors/oversights)