From our sponsor: Elevate all your marketing with Mailchimp Smarts.
I was wondering how to get images from a site the same way like Digg or Facebook do, where it is only necessary to provide a link, and images get automatically uploaded to your server and resized. So, I decided to create a script that does exactly that and share it here.
This PHP script uses two php classes that I found on the web. One of them I have already used before in the “jQuery Image Cropper with Uploader” and the “Dynamic jQuery Image Gallery with Uploader“, the class.upload from verot . The other is a PHP html DOM Parser that can be found here.
In order to make this work, I had to change some few lines of code in verot’s class, so that URLs can be used to get images. The DOM Parser is untouched. I also created a file (index.php) where both scripts are called. I first parse the URL, search for images and pass the url of the image to the class.upload script, which then uploads the image to a local directory (images/) and resizes it. In this script, the image size is changed to a width of 150 pixels but the ratio is kept. However, you can change these values and configure it the way you want. Take a look at the documentation of the class.upload so that you know what you can change. Also, you can specify how many images you want to save / display per request.
For the demo (not available anymore, sorry!) which has this blog as “target”, I have configured that only a restricted amount of images are saved and shown. Besides, you might notice that sometimes it displays more images than other times. This is caused by the limited amount of memory configured in the php.ini file (memory_limit), so you might wanna change this value to a proper one if you want to use this script. Again, on verot’s web site, in the forums, there are some good calculations giving an idea of a suitable value.