This is the first in a series of accepting a users image, selecting the area, and cropping it. First we will start with the guts of the process and build on top of that.
When letting users upload images for a site, you normally would like the images to be the same ratio and size. To do this we need to crop the image. To get things started lets select the image.
Now that we have the image we need to load the image into GD before we can do anything with it. You can find all the GD functions we use here. (more…)
Memcache can be great to store variables, but beware of Memcache::flush. The function actually requires about 1 second to finish. Below is the best way to handle it:
1
2
3
4
5
6
7
$oMemcache->flush();// Wait for memcache to finish flushing$time=time()+1;//one second futurewhile(time()<$time){//sleep}
DISCLAIMER: I would like to say I do not condone doing this. Better ways, more legal, ways to get content from someone. But sometimes this is asked of you by your boss. DO NOT STEAL CONTENT.
For this weeks Thursday Code Tip I will show how to use PHP to crawl a website to gather content. First we start by selecting the URL to crawl:
1
$sURL="http://www.defvayne23.com/";
Next we get the content of the page:
2
$sContent=file_get_contents($sURL);
Now to use REGEX to get what we want. You can learn patterns here. Below we search for the text within a H1 tag.
Working with classes in PHP is not only great for keeping your framework clean, but handling an item with ease. The makeImage helper for Kaizen CMS makes use of this. It’s easy to make changes to the image after you create the class object as below. Below code re-sizes an image, then crops to fit within an image restraint.
1
2
3
4
5
6
7
8
// Pass the file path and option to cache the image to the class$oImage=new makeImage($sFile,true);// Tell the object to resize the image, no need to pass the image$oImage->resize(200,100);// Now crop the image after it was resized$oImage->crop(100,100,0,0);// Now send the image data to the user$oImage->draw(null,100);
Class handled all the dirty work of keeping up with the image, and knowing all the info about the image. Above code is easier to maintain then doing all the code straight in the file. Class used above is available here.
Working on Hospice of Wichita Falls I used my image resize function that’s built into the my CMS. Was having trouble with the gallery and the images not showing up. After some short debugging, realized the code was dying when I tried to make sure the file existed using is_file before I passed it onto GD. I spent hours looking at the path for the file and making sure everything was spelled correctly and that the file in fact did exist. (more…)