Issues having Facebook profile images - php

I have been searching the answer for my question for hours but I couldn't find an easy way to do it.
I'm building an Android application using Java and PHP to show, update, insert and delete data from my Database tables. I'm using parameters to interact the Java with the PHP.
My application is using the Facebook sdk for the Login, the users can login and create their accounts using the facebook.
I'm getting the E-mail, name and profile image from every user that tries to create an account on my App.
But I'm getting some issues when I try to get the Image profile from the users. I'm receiving the URL like that:
http://graph.facebook.com/ID_FROM_THE_USER/picture?type=large
I could easily get these images only using Java, but I need to save the Image into a specific directory using PHP. I'm following these steps.
Receiving the image URL from the profile.
Passing the Image URL to my PHP file using parameters.
Saving the Image into a new directory on my local computer.
Inserting into my database the name from the Image, and the directory from the folder where the Image is located.
And I'm stuck on the third step because my code can't recognize that I am sending an Image.
My code to receive the Image:
define('DIRECTORY', 'test');
$content = file_get_contents($image); //$image = received Image
$parts = explode('.', $image);
$new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . "jpg";
$small_percent = 0.5;
$thumb_percent = 0.2;
list($width, $height) = getimagesize($image); //Resizing the image
$small_new_width = $width * $small_percent;
$small_new_height = $height * $small_percent;
$thumb_new_width = $width * $thumb_percent;
$thumb_new_height = $height * $thumb_percent;
//Saving to my directory
file_put_contents(DIRECTORY.'/' . $new_url , $content);
resizeImage($image, DIRECTORY.'/small_' . $new_url, $small_new_width, $small_new_height);
resizeImage($image, DIRECTORY.'/thumb_' . $new_url, $thumb_new_width, $thumb_new_height);
Image resize function:
function resizeImage($source, $dest, $new_width, $new_height)
{
list($width, $height) = getimagesize($source);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($source);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $dest, 100);
}
What I have tried by far:
I read some Questions from three years ago and the solution was that:
$url = 'http://example.com/image.png';
$img = '/my/folder/flower.png';
file_put_contents($img, file_get_contents($url));
But unfortunately is not working anymore.
Thank you.

Related

Security concerns with base64 encoded images

I have an API that accepts base64 encoded image data, and will need to decode the data, save the image file, then create a thumbnail from that image.
I am concerned that malicious code could get executed if I do not properly validate the contents of the POST payload before attempting to create a thumbnail.
The basic workflow I have thus far is below. Is there enough validation that I do not need to be concerned about security? I guess I am worried about someone encoding something bad, then when one of the image functions below is called, the internet explodes.
<?php
$decodedImage = base64_decode($_POST["canvas"]);
if ($decodedImage === false) {
// Error out
}
$imageSizeValidation = getimagesizefromstring($decodedImage);
if ($imageSizeValidation[0] < 1 || $imageSizeValidation[1] < 1 || !$imageSizeValidation['mime']) {
// Error out
}
$tempFilePath = "/tmp/" . microtime(true) . "-canvas-web.jpg";
file_put_contents($tempFilePath, $decodedImage);
$originalWidth = $imageSizeValidation[0];
$originalHeight = $imageSizeValidation[1];
$newWidth = 49;
$newHeight = 49;
$scaleWidth = $newWidth / $originalWidth;
$scaleHeight = $newHeight / $originalHeight;
$scale = min($scaleWidth, $scaleHeight);
$width = (int)($originalWidth * $scale);
$height = (int)($originalHeight * $scale);
$xpos = (int)(($newWidth - $width) / 2);
$ypos = (int)(($newHeight - $height) / 2);
$oldImage = imagecreatefromjpeg($tempFilePath);
$newImage = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($oldImage, 255, 255, 255);
imagefilledrectangle($newImage, 0, 0, $width, $height, $background);
imagecopyresampled($newImage, $oldImage, $xpos, $ypos, 0, 0, $width, $height, $originalWidth, $originalHeight);
imagedestroy($oldImage);
imagejpeg($newImage, "/path/to/new.jpg", 90);
imagedestroy($newImage);
Didn't end up getting any answers, so for those that are interested in what I ultimately did:
After investigating this a bit more, I found that one of my biggest concerns was valid image files encoded with inline PHP, Ruby, etc. EG: An image with the following at the end:
<?php phpinfo();
I ended up taking the decoded image data, and giving it to imagecreatefromstring(), then saving the image to a temp directory via imagejpeg().
This seemed to remove any encoded PHP from the original image data. At that point I validated the image size data of the saved image using getimagesize(). Assuming that everything was valid at that point, I moved the image to a permanent location.
Another thing I changed, was instead of using a filename based on a static string and microtime(), I used a hash.
Regarding the concern of images with code injected, I found this link to be helpful:
https://www.owasp.org/index.php/Unrestricted_File_Upload
I also found this other SO post useful, as far as overall ideas go:
Validating base64 encoded images
Finally, the following book brought the concern of images with code to my attention in the first place:
http://www.apress.com/9781430233183

PHP resize image from FTP

I need resize Image from FTP like /images/1_2.jpg. I need to load it to imagecreatefromjpeg();. Now my script looks like this.
$file = "images/1_2.jpg";
$imagesize = getimagesize($file);
$img = imagecreatefromjpeg($file);
$width = 70;
$height = 100;
$img2 = imagecreatetruecolor($width, $height);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $width, $height, $imagesize[0], $imagesize[1]);
imagejpeg($img2, "images_mini/1_2.jpg");
My problem is that images don't upload to FTP, its shows me everything is ok but i can't find images on FTP.
Sorry I have it, just only bad converted variable, i using cross languages now for application so it is dificult to check everythink. Thank you anyway.

How to get image thumbnail from given link?

When sharing link on Facebook, Facebook get Title,Meta Description and Image from given link.
I have no idea how they get the image. I search on the internet as well as, but I could n't find a way.
How to get image thumbnail from given URL?
Well, save file to your server using file_put_contents(), and then you can create thumbnail. you can't get thumbnail directly from url.
Save file like this :
$url = 'http://example.com/image.ext';
$img = 'yourImgNameOrWithPath.ext';
file_put_contents($img, file_get_contents($url));
Create thumbnail using following code :
function createThum($filename,$thumb_width,$thumb_height,$destination)
{
$my_input_file = $filename;
$my_output_file = $destination;
$jpeg_quality = 100;
$size = getimagesize($my_input_file);
//$thumb_width = ($size[0] / $size[1]) * $thumb_height;
$src_img = imagecreatefromjpeg($my_input_file);
$dst_img = imagecreatetruecolor($thumb_width,$thumb_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]);
imagejpeg($dst_img, $my_output_file, $jpeg_quality);
imagedestroy($src_img);
imagedestroy($dst_img);
return true;
}

Reading image from a local temporary file using php

Scenario : Earlier I was reading the url of image directly and resized the image into 4 different size . But I had execution time out . Now I read the url and copy it into a temporary folder and pass the images in local temporary folder to imagecreatefromjpeg().
protected static function saveImage($row,$url){
$percent = 1.0;
$imagethumbsize = 200;
$db = PJFactory::getDbo();
$details = $db->getImageDetails();
$max = sizeof($details);
$tempfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."temp".DS.$row['CategoryID'].".jpg";
$tempcopy = copy($url,$tempfilename);
foreach ($details as $array) {
$new_width=$array[2];
$new_height=$array[3];
$newfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."c".DS.$row['CategoryID']."-".$array[1].".jpg";
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($tempfilename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $newfilename);
}
}
Error : The images are correctly being saved in temp folder . But in the destination folder images of all sizes are created but the image looks only black . (Not getting the actual image) . I guess there is some problem with the file reading from local .
Any idea ?
Thanks in advance.
If you are trying to read files other than JPG files, it will return only black images,
So while reading images, check which filetype your file really has and then call the correct function between the three (jpeg, png, gif).
Do you set $width and $height anywhere?
$width = imagesx($image);
$height= imagesy($image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

Save an image Resized with PHP

I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038

Categories