Image convert

Is it possible to convert all image uploads to database to png format ?

While it’s not the best practice to store large files in database (such as images), it is definitely possible to store them in your tables in the form of say, base64.

the file size is preset to stop large files just need every image to have png file extension

As was said, it is a horrible design to save them in a database. But, it can be done. You can use PHP image functions to convert the incoming images in just a few lines of code. You can convert it in one line like this:

imagepng(imagecreatefromstring(file_get_contents($filename)), "newfile.png");

You would need know the original name of the file, create an " Image Object " using the function for that and then convert the image object to a PNG and then save it. You might want it be saved as the same name but in the example, it is just called newfile…

Oh, by the way, to save the file as a BLOB field, you would need to convert it to a serialized value and also base64 as Jpajak mentioned. The reason we normally do not do this is to save server resources which are overpowered by all the conversions. Normally, you just save a pointer to the image in a folder and load it directly from there as needed. Hope this helps…

Sorry i not explained properly the images are saved in a folder on the server. When a user signs up they upload a profile image. At the moment the images are renamed to suit user id.

For example if a new user signs up and next auto increment is say 2 and they upload a png image it will be renamed 2.png if then another user signs up and uploads a jpg image it will be renamed 3.jpg and so on.

What i want is for all the images that are uploaded to be converted to png whether they are gif, bmp, jpg etc

Good. That process is a good way. So, you can use command I gave you. You would just need to change the “newfile.png” to $id.".png" or something like that to change the name of it. But, the functions will work for you.

thank you for your help

Sponsor our Newsletter | Privacy Policy | Terms of Service