I have some code that is supposed to allow the user to browse his machine for jpeg, gif, or png to upload.
My code works fine from my xampp localhost.
I have upload my code to Amazon Web Services where I have a Ubuntu 12.04 server that I installed a LAMP on. Once there the code does not work.
I have tracked down where I believe the code fails. It is at getimagesize($old_image_path) this returns a empty set.
$old_image_path = /var/www/ch23_ex1/images//leaf.gif when I select a pic from my desktop, not sure why this path shows but it worked with xampp.
Notice the echo statements that included for testing the output from these is:
allow_url_fopen = 1
/var/www/ch23_ex1/images//NChandler03.jpgbool(false) Image type = File must be a JPEG, GIF, or PNG image.
Here is the section of code where I believe the fail is:
function resize_image($old_image_path, $new_image_path,
$max_width, $max_height) {
echo "allow_url_fopen = " . ini_get("allow_url_fopen")."<br>";
// Get image type
echo $old_image_path;
//$image_info = file_get_contents($old_image_path);
$image_info = getimagesize($old_image_path);
$image_type = $image_info[2];
var_dump($image_info);
echo 'Image type = '.$image_type;
// Set up the function names
switch($image_type) {
case 2:
$image_from_file = 'imagecreatefromjpeg';
$image_to_file = 'imagejpeg';
break;
case 1:
$image_from_file = 'imagecreatefromgif';
$image_to_file = 'imagegif';
break;
case 3:
$image_from_file = 'imagecreatefrompng';
$image_to_file = 'imagepng';
break;
default:
echo $image_type;
echo 'File must be a JPEG, GIF, or PNG image.';
exit;
}
}
I found my answer posted on similar question. All I had to do was change the permissions on the images file by entering the following code into the server. This first command changes the directory to the file that holds my images file. The second command changes the permissions of the images file so that my code can write to it. I hope this helps someone else.
cd /var/www/ch23_ex1/
sudo chmod 775 images
Related
I have a very strange problem.
On my website there is a file field that will allow the users to upload their profile picture.
It gets uploaded using JQuery and it gets saved using PHP.
If I upload from a PC / MAC / iPhone then there is no problem whatsoever, however if I upload using an Android device the image gets rotated.
The rotation is not even consistent, it could be 90% 180% or 270%, this happens when taking a image or selecting from the Gallery.
Why would this happen? and is there a possible fix?
This solved the issue
From the PHPDocs
<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>
I am following this tutorial: Tutorial
In the PHP script it uses the GD library to move and save files.
This script is working fine when i am using it on my local WAMP server.
But when I upload it to my hosting on YourHosting it does not, When i use a JPEG or PNG file it works but when I use a .JPG file it does not work.
This is part of the code:
$sTempFileNameFirst = TEMPLATEPATH;
$sTempFileNameFirstSrc = get_bloginfo('template_directory');
$sTempFileNameLast = '/cache/' . md5(time().rand());
$sTempFileName = $sTempFileNameFirst . $sTempFileNameLast;
$sTempFileNameSrc = $sTempFileNameFirstSrc . $sTempFileNameLast;
// move uploaded file into cache folder
move_uploaded_file($_FILES['upload-image']['tmp_name'], $sTempFileName);
// change file permission to 644
#chmod($sTempFileName, 0644);
if (file_exists($sTempFileName) && filesize($sTempFileName) > 0) {
$aSize = getimagesize($sTempFileName); // try to obtain image info
if (!$aSize) {
#unlink($sTempFileName);
return;
}
// check for image type
switch($aSize[2]) {
case IMAGETYPE_JPEG:
$sExt = '.jpg';
echo("jpg");
// create a new image from file
$vImg = #imagecreatefromjpeg($sTempFileName);
break;
case IMAGETYPE_PNG:
$sExt = '.png';
// create a new image from file
$vImg = #imagecreatefrompng($sTempFileName);
break;
default:
#unlink($sTempFileName);
echo("unlink");
return;
}
echo("before truecolor");
// create a new true color image
$vDstImg = #imagecreatetruecolor( $iWidth, $iHeight ) or die ("can't open gd stream");
echo("after imagetruecolor");
// copy and resize part of an image with resampling
imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['width'], (int)$_POST['height']);
echo("after resample");
// define a result image filename
//$sResultFileName = $sTempFileName . $sExt;
$sResultFileNameSrc = $sTempFileNameSrc . $sExt;
echo($sResultFileNameSrc);
// output image to file
imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
#unlink($sTempFileName);
return $sResultFileNameSrc;
As you can see I use echo to see where it works and where it does not. So for JPEG and PNG files every code works, but whenever i use a JPG file the echo before #imagecreatetruecolor works but after does not work. So the problem is that that function does not execute. Also the "or die" part after the function does not work.
What could be the problem?
PHPINFO() says GD info is enabled and has bundled version(2.1.0)
User uploaded images account for a large portion of the content on the site I'm working on. I tried storing them outside of the webroot and fetching them with readfile() for security reasons, but it was just too slow so I had to go back to the old method.
Now I'm looking to make sure all uploads are 100% sanitized since they'll be stored inside the webroot. My question is, if a user were to rename a harmful script to a .jpg, .gif, .png, or .bmp and uploaded it, would it still be harmful when executed or fetched if the image was recreated with a function like this:
function imageCreateFromAny($filepath) {
$type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize()
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
6 // [] bmp
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($filepath);
break;
case 2 :
$im = imageCreateFromJpeg($filepath);
break;
case 3 :
$im = imageCreateFromPng($filepath);
break;
case 6 :
$im = imageCreateFromBmp($filepath);
break;
}
return $im;
}
In other words, is there anyway to trick one of the imagecreatefrom* functions into executing content as a script instead of an image or would even a harmful script that's been run through this be reduced to a broken image?
First time uploading image and getting this error. Image is .jpg. Script seems to be OK for me. So I think problem is with xamp server?
Warning: imagecreatefrompng(): 'C:\xampp\tmp\phpB42E.tmp' is not a
valid PNG file in C:\xampp\htdocs\phphph\check_image.php on line 66
The file you uploaded was not a supported filetype
I was searching on google and didnt find something usefull. So here is part of the script.
switch ($type){
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or
die ('The file you uploaded was not a supported filetype');
$ext = ' .gif';
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or
die ('The file you uploaded was not a supported filetype');
$ext = ' .jpeg';
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or
die ('The file you uploaded was not a supported filetype');
$ext = ' .png';
break;
default:
die('The file you uploaded was not a supported filetype.');
}
Missing break:
$ext = ' .jpeg';
/// missing break here
case IMAGETYPE_PNG:
So you upload a jpg, and the code continues on into the PNG section, hence your error.
And so, no, it's not a problem with Xamp server... It's a PEBKAC error.
I have a iPhone app that uploads pictures to my server. One major issue I am having is a rotating one.
For some reason if I upload a picture from my iPhone, some pictures will automatically rotate. The one's that do get rotated are the ones in portrait mode. I have no code in my script that rotates the images.
How does a server exactly process tall images? Should I modify my php file to check to rotate it ahead after it automatically rotates? Should I code something in my iPhone app that will check this?
Any help is appreciated!
PS: If you need code, feel free to ask!
Some pictures(jpg) have exif data that tells the position the camera was when the picture was shot.
Take a look at http://www.php.net/manual/en/function.exif-read-data.php#76964
You may rotate the pictures server-side like this
Or a better way is to use this library
https://github.com/Intervention/image
And simply use like this-
$img = Image::make('foo.jpg')->orientate();
More can be found here.
When you take a picture your phone saves any rotation metadata in EXIF headers. When you upload the image to your server, that metadata is still sitting there but it's your job to apply it to the image to rotate it (if you want). In PHP you can use a function called exif_read_data:
function correctImageOrientation($filename)
{
$exif = exif_read_data($filename);
if ($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if ($orientation != 1) {
$img = imagecreatefromjpeg($filename);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img, $deg, 0);
}
imagejpeg($img, $filename, 95);
}
}
}
To use it simply call the function after you save the file. For more info and an additional PHP solution see the original source.