I'm working on a PHP project, and I record Images DATA URI in a table called "Images", the thing is that that long string takes lot of place for nothing so its heavy to load.
My questions :
How can I resize a Data URI image using PHP (knowing that the input and output wanted will be both DATA URI)
Is there any function or something alike that can compress the DATA URI before put it on my MySQL Database ? (Compress decompress)
There is a library that will do this, you can get it here.
If you want to roll your own solution, PHP allows you to open data URIs with the various file* functions. If you don't mind saving the file to disk while you work on it, you can just do this:
$fileName = md5($dataURI);
file_put_contents($fileName, file_get_contents($dataURI));
Then you can use your preferred image processing library to manipulate the image. To send it again as a data URI, you can use the technique outlined here. Then, delete the temporary file and you're done.
Related
I created an image class, it loads and resizes image, then crops and lastly watermarks it.
For last step, I would like to add IPTC data to the this image. For what I know there is only iptcembed function to accomplish this. The problem is iptcembed needs the path of image as parameter. I'm keeping image as object before using it to render views.
$content = iptcembed($data, "./image.jpg");
I do not want to save image to storage just to create a path for iptcembed but I couldn't find another way to add IPTC data to the image.
Is there any way (or function) to access to this object from memory instead of saving and loading from storage?
You could use php://memory to have a file handle that reference in-memory data instead of data written to a file.
Credit goes to this original answer
I use CFX_OpenImage to read and write IPTC_ data in .jpg files using ColdFusion Language versions 8 thru 11. I also use CFX_OpenImage for image resize and rotation.
For more CFX_OPENIMAGE INFO go to http://www.kolumbus.fi/jukka.manner/cfx_openimage/
The software download includes a good manual of at least 65 pages.
I have an image resource that is manipulated with imagecopyresampled. I need to pass that image to a set of methods that expect a string input, not a resource. But I don't need to store the file locally.
Is this the proper way:
Store the image with imagepng and imagejpeg
Pass string (filename) to the methods
Destroy the stored file with #unlink
Is that right? Seems sloppy.
Note: the image is not coming from a file upload and hence can't be accessed with $_FILES["Filedata"]["tmp_name"]
I took a look at the Amazon S3 PHP API:
http://docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/upload_part
I assume you are using something like the upload_part method that takes a string filename. In that case, unless you plan to modify their library, you will need to store the file to disk and pass them the filename so they can read the file and perform the upload.
Besides the steps mentioned in your question you can take a look at imagedestroy to make sure you are freeing up the memory for your image resource after it is written to disk with imagepng. And then, as you stated, you can delete your temp file with unlink after your upload is complete.
I agree, it does seem a bit wasteful, but in this case necessary since the API doesn't seem to provide an alternative.
Just a quick question I am wanting to allow users to link there facebook account to my new product, and I am wondering how do I take there facebook photo URL and save it as an image in BLOB formate for MYSQL
I am using this example to connect to Facebook
https://github.com/facebook/connect-js/blob/master/examples/jquery/login.html
and to send the image to my server I use the $.ajax formate to submit it to the core.php (I have not coded this yet, as I need to know the best way.
Well my first recommandation is to NOT store image data in your database unless you absolutely need that in your database backups. You will clutter your database massively. Instead, just get the url to the picture using the server-side $facebook class, and just "file_get_contents" it to your php memory.
Then dump it on disk and save a reference of that dumped image in your database. It will do much more good to your database this way.
Else, if you really want to save it to a BLOB, use the same method to fetch the image to memory and then use the hex() function to transform it to a textual representation that can be fit into an INSERT/UPDATE query...
Just save a reference to the location where Facebook stores the image. That way you can abuse their servers (which are very nice/fast) and just serve image tags that refer to their servers.
Also when you save images to a database you lose the native compression that you get when you save an image to the filesystem, so you bloat your data storage size for not much reason (backups are easier I suppose).
hello i am trying to use Codeigniter to build a simple user image gallery where a user can upload an image and then this will be auto displayed on another page.
I have the image uploading done as explain on the user guide on Codeigniter and this works fine dumps the image in a folder in the root just placing the URL in one table area and echoing this out works but this will not cover all image names.
what I would like to know is there a way of reading what the user has uploaded and auto storing this in the database to be echoed out.
if not what's the best practice way to do this?
ps new to codeigniter framework but quite familiar to PHP mysql many thanks.
When uploading data using the uploading class you can retrieve information about the uploaded file using the following:
$this->upload->data();
This will return an array containing the name, path, dimensions, etc.
I am not totally familiar with the Codeigniter framework, but from a standard PHP/MySQL perspective this can be done using a BLOB field with MySQL. Simply fread() the uploaded image (addslashes to the content before sending the data) and send that binary output to the BLOB field with a standard SQL query. After that, you will probably need to create a separate file to actually call the image back (PHP will need to reconstruct the image from the data). Familiarize yourself with the HTTP-HEADER "Content-Type:" and how it works for images.
For instance, if you stored a .jpeg image to the database, you will want to set
header("Content-type: image/jpeg");
echo $query_string_array['Image_Data_Row'];
This would need to be in its own file (or part of file ?image=some_img) so it can display only the image. But that is the basic concept of storing an image to the database; you simply break it down, store the information, and reconstruct it when you need it.
Regards,
Dennis M.
As Yorick alluded to: after uploading the file, you can access the file path with the $this->upload->data() method. Specifically, $this->upload->data('full_path'). After the upload, assign this to a variable, then send it to a model or insert the path directly into the db. This will be the full file path to the image on your system. If you want a http accessible path stored instead, you can do something like:
$this->load->helper('url');//load url helper if not autoloaded
$fileName = $this->upload->data('file_name');
$httpPath = base_url().'uploadFolder/'.$fileName;
$this->db->insert('image_table',array('path'=>$httpPath));
HTH
I am in the middle of making a script to upload files via php. What I would like to know, is how to display the files already uploaded, and when clicking on them open them for download. Should I store the names and path in a database, or just list the conents of a directory with php?
Check out handling file uploads in PHP. A few points:
Ideally you want to allow the user to upload multiple files at the same time. Just create extra file inputs dynamically with Javascript for this;
When you get an upload, make sure you check that it is an upload with is_uploaded_file;
Use move_uploaded_file() to copy the file to wherever you're going to store it;
Don't rely on what the client tells you the MIME type is;
Sending them back to the client can be done trivially with a PHP script but you need to know the right MIME type;
Try and verify that what you get is what you expect (eg if it is a PDF file use a library to verify that it is), particularly if you use the file for anything or send it to anyone else; and
I would recommend you store the file name of the file from the client's computer and display that to them regardless of what you store it as. The user is just more likely to recognise this than anything else.
Storing paths in the database might be okay, depending on your specific application, but consider storing the filenames in the database and construct your paths to those files in PHP in a single place. That way, if you end up moving all uploaded files later, there is only one place in your code you need to change path generation, and you can avoid doing a large amount of data transformation on your "path" field in the database.
For example, for the file 1234.txt, you might store it in:
/your_web_directory/uploaded_files/1/2/3/1234.txt
You can use a configuration file or if you prefer, a global somewhere to define the path where your uploads are stored (/your web directory/uploaded files/) and then split characters from the filename (in the database) to figure out which subdirectory the file actually resides in.
As for displaying your files, you can simply load your list of files from the database and use a path-generating function to get download paths for each one based on their filenames. If you want to paginate the list of files, try using something like START 0, LIMIT 50; in mySQL. Just pass in a new start number with each successive page of upload results.
maybe you should use files, in this sense:
myfile.txt
My Uploaded File||my_upload_dir/my_uploaded_file.pdf
Other Uploaded File||my_upload_dir/other_uploaded.html
and go through them like this:
<?php
$file = "myfile.txt";
$lines = file($file);
$files = array();
for($i=0;$i<=count($lines)-1;$i++) {
$parts = explode($lines[$i]);
$name = parts[0];
$filename = parts[1];
$files[$i][0] = $name;
$files[$i][1] = $filename;
}
print_r($files);
?>
hope this helps. :)
What I always did (past tense, I haven't written an upload script for ages) is, I'd link up an upload script (any upload script) to a simple database.
This offers some advantages;
You do not offer your users direct insight to your file system (what if there is a leak in your 'browse'-script and you expose your whole harddrive?
You can store extra information and meta-data in an easy and efficient way
You can actually query for files / meta-data instead of just looping through all the files
You can enable a 'safe-delete', where you delete the row, but keep the file (for example)
You can enable logging way more easily
Showing files in pages is easier
You can 'mask' files. Using a database enables you to store a 'masked' filename, and a 'real' filename.
Obviously, there are some disadvantages as well;
It is a little harder to migrate, since your file system and database have to be in sync
If an operation fails (on one of both ends) you have either a 'corrupt' database or file system
As mentioned before (but we can not mention enough, I'm afraid); _Keep your uploading safe!_
The MIME type / extension issue is one that is going on for ages.. I think most of the web is solid nowadays, but there used to be a time when developers would check either MIME type or extension, but never both (why bother?). This resulted in websites being very, very leaky.
If not written properly, upload scripts are big hole in your security. A great example of that is a website I 'hacked' a while back (on their request, of course). They supported the upload of images to a photoalbum, but they only checked on file extension. So I uploaded a GIF, with a directory scanner inside. This allowed me to scan through their whole system (since it wasn't a dedicated server; I could see a little more then that).
Hope I helped ;)