I'm trying to convert a user-uploaded PDF file to .jpg, to create some sort of thumbnail. I want the thumbnail to be displayed on the same page once they upload their PDF.
I'm using Imagick.
Here's some of my code:
$filePath = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$pdfThumb = new Imagick();
$pdfThumb->setResolution(300,300);
$pdfThumb->readImage($filePath . '[0]');
$pdfThumb->setImageFormat('jpeg');
$fp = $fileName . '.jpg';
$pdfThumb->writeImage($fp);
I don't feel like anything is really happening, and I want to make sure it saves the thumbnail to my server (possibly temporarily) and show it on a div. Any tips would be much appreciated!
Related
how do rename an image file and then upload with resize image see below my php code
move_uploaded_file($_FILES["img"]["tmp_name"], "bannerimg/" . $image);
$image = new SimpleImage();
$image->load("bannerimg/" . $_FILES["img"]["name"]);
$image->resize(520,310);
$image->save("bannerimg/thumbs/" . $_FILES["img"]["name"]);
I think it's simple? This one does exactly what you want: PHP rename()
rename($_FILES["img"]["name"], "new_name.jpg");
And here you go, then you may do whatever you want. :)
If this is a newby question, forgive me. I have coded a php file uploader. After hearing that hackers could attatch/disguise code to images to infect sites, I got an idea to solve that problem. To convert the upload image to another file format (png) and then to another (jpg) with 70% quality. This caused the malware to become corrupted. The problem is, this total conversion process takes a about 1 minute at top speed. The service I'm making needs to be quick to handle the file uploads so that the users can go about the work. How can I speed up this process? The upload code is below (important variables are blanked).
// upload the file
$status = "...recieving the file...";
move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);
$status = "...processing the file...";
// convert the file to destroy viruses
$filename21 = "$folder" . $_FILES["file"]["name"];
imagepng(imagecreatefromstring(file_get_contents($filename21)), "$folder"."debug.".$picture_newname.".png");
$imageTmp=imagecreatefrompng("$folder"."debug.".$picture_newname.".png");
imagejpeg($imageTmp, "$folder".$picture_newname.".jpg", 90);
imagedestroy($imageTmp);
These are the steps it follows:
Scan database for file with the same name
if file with same name is found, rename the current upload
receive the file
"evaluate" the file (the double conversion process to protect the server)
insert the info into the uploads database
If any other codes are needed (or if i should do some more timing) please let me know. Thanks in advance for your assistance.
This is a crazy idea. You're not just tying up the server in converting between image formats, you're also degrading the quality of every uploaded image.
I'd recommend a different approach
When a file is uploaded, use PHP's getimagesize() function to check the image. If this function returns FALSE (or an unexpected image type, or strange dimensions, etc.), then the file is corrupt and can be deleted.
Use exiftool or something similar to strip away all the metadata from the uploaded file before you store it away on the server. That way you can ensure that the file only contains image data.
Perhaps you could check that the value of $_FILES["file"]["name"] doesn't contain anything sneaky like ../../ before you use it to save the file on your server.
It's totally bad idea implement double conversion for security purpose, because of DoS attack.
Balanced solution between speed & security must contain:
Check MIME type.
Check file extension.
Check file size. (highly recommended)
Check image size. (optional, depends on application requirements)
Something like this:
$allowable_types = array(
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif'
);
$max_file_size = 10240; // 10K limit example
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type = $finfo->file($_FILES['name']['tmp_name']);
$size = filesize($_FILES['name']['tmp_name']);
$info = pathinfo($_FILES['name']['tmp_name']);
if (isset($allowable_types[$info['extension']])
and $type === $allowable_types[$info['extension']]
and $size <= $max_file_size
) {
// check image size if your app require this
move_uploaded_file($_FILES['name']['tmp_name'], $destination);
}
Update
I would not recommend to use $_FILES['file']['name'] in destination path or scan whole directory for same name. Because of some security flaws and performance drop. Better solution is to generate unique name for each image:
$new_name = uniquid() . '.' . $info['extension'];
$destination = $upload_path . $new_name;
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.
I have a database of 1000 items with one field being 'images'. This is a text field with the name of a particular image file (PNG or JPG). I'm trying to find a way to include an 'upload' button on my website so that users can upload an image to the server, and it be recorded in the MySQL database.
I've looked at several ideas online and not found anything that either suits my needs or I can get working. All images can be stored in the same folder as they will all have different names.
Does anyone know of any tutorials, or can assist me in doing it, many many thanks x
Read this tutorial ...and also check did u give enctype="multipart/form-data" in form...
this is php script to upload image:-
if ($_FILES['inputfieldname']['name']) {
$filename = stripslashes($file[inputfieldname]['name']);
$extension = "get the extension of file";// jpg if image format is jpg
$extension = strtolower($extension);
//set target image name
$image_name1 = date("Ymdhis") . time() . rand() . '.' . $extension;
$target = "target directory path";
if ($this->checkExtensions($extension)) {
$filestatus = move_uploaded_file($file[inputfieldname]['tmp_name'], $target);
#chmod($target, 0777);
if ($filestatus) {
// insert $image_name into database
}
}
}
I am looking to accomplish this in CodeIgniter specifically.
The PHP App I am coding allows a user to upload either a jpg or an animated gif image. On the next step I want to allow the user to use jCrop to crop a few different size thumbnails. This would require me to convert a new copy of the animated gif to a jpg. My code works fine with uploaded jpg images, but creates a broken image for gif files. Is what I am trying to do possible?
My Code:
// Create image to crop
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $this->config->item('upload_dir_path') . $file_path . 'original.' . $file_ext;
chmod($config['source_image'], 0777);
$config['new_image'] = $this->config->item('upload_dir_path') . $file_path . 'crop-me.jpg';
$this->image_lib->initialize($config);
$this->image_lib->resize();
For those interested in my solution, I simply used the built in GD PHP functions. I have little experience dealing with gif files so I was expecting this to be difficult. The fact of the matter is the CodeIgniter Image_lib and extended library (Which I never got to work properly) is overkill for this.
Here is what I used:
$image = imagecreatefromgif($path_to_gif_image);
imagejpeg($image, $output_path_with_jpg_extension);
Very easy and worked perfectly for what I needed.