Bravo! Bravo! I knew I can do it. Stayed up late last night, learning PHP code. Haha! I'm no George Malley (from Phenomenon, John Travolta), who devoured an English-Spanish dictionary on a 20 minute drive to a sick lady they all thought was possessed (stomach flu can be the devil), but last night, I set out to accomplish something and today I woke up to find out that it actually worked.I am very much in love with my plain and simple theme. But what readers do not see is underneath this smooth exterior, there's a violent monster ruthlessly cleaning up content, a.k.a. wordpress's plugin system. To give you a brief picture, let me explain how images are handled in my blog.
First of all, I am extremely lazy. I hate downloading images off unassumming websites, just to re-upload them into mine. I find this completely unnessary. If we are to steal images, I say, why not do it in style? So I spent hours and hours (ironic huh? lazyness pushing me to work for hours! happens all the time) looking to a plugin that would automatically download remote images. The answer? wp super images.
I know I out moved a lot of you guys. I had been searching for the plugin and most of the forums show unresolved queries for a such and such plugin. I found it. Here it is. I think, it's lack of popularity might be due to its EASL developers, Chinese if I'm right.
So that's one part that's cool! The plugin downloads images from "img" tags and links to images (eg a href equals blah blah). It treats both queries separately too. So if you wanna have a hand on things like me, it'll be easy for you.
Secondly, once you have all your images on your local server (was that a misnomer or whats that you call names for ironic things like huge minitures), you need to make sure they are never too big for your theme. I have nCode Image Resizer for that.
nCode Image Resizer is another awesome simple program. My problem is resizing is done using javascript. That means readers would first download the huge files and then javascript would check them for obesesity and only then will it reduce them. Not good! Now, I remember there was a popular non-wordpress-exclusive script that generates thumbnails off images with width and height arguments. TimThumb. Last night, I decided to write php functions on for my theme that would measure images widths. Once it finds obese pics, it'll make some calculations to find the proper height for the new girth a.k.a BMI and then replace the src links from my image tags to incorporate timthumbs new diet dimensions.
And viola! I can upload as many pics as I want. Pics of all sizes. And my theme would search them out, weigh them and resize them if neccessary. I'll have a plugin released maybe next week, if time permits.
add_action('the_content', 'JD_treatcontent');
function JD_treatcontent ($the_content) {
//this looks for all img tags from the_content
preg_match_all('/<img[^>]+>/is',$the_content, $result);
//creates two arrays, newimgtags is empty, originalimgtags contains the img tags
$originalimgtags = $result[0];
$newimgtags = array();
//each tag is passthru jd_capimagesize function and results is sent to the empty array newimgtags
foreach ($originalimgtags as $key=>$imgtag) {
$newimgtags[$key] = JD_capimagesize($imgtag);
}
//each instance from the original list of tags is replaced with the new one
$post->post_content = str_replace($originalimgtags,$newimgtags,$the_content);
return $post->post_content;
}
function JD_treatimages ($post_ID) {
$post = get_post($post_ID);
$the_content = $post->post_content;
//this looks for all img tags from the_content
preg_match_all('/<img[^>]+>/is',$the_content, $result);
//creates two arrays, newimgtags is empty, originalimgtags contains the img tags
$originalimgtags = $result[0];
$newimgtags = array();
//each tag is passthru jd_capimagesize function and results is sent to the empty array newimgtags
foreach ($originalimgtags as $key=>$imgtag) {
$newimgtags[$key] = JD_capimagesize($imgtag);
}
//each instance from the original list of tags is replaced with the new one
$post->post_content = str_replace($originalimgtags,$newimgtags,$the_content);
//database is updated
global $wpdb;
$postary=add_magic_quotes(array("content"=>$post->post_content));
$wpdb->query("UPDATE `wp_posts` SET `post_content` = '{$postary["content"]}' WHERE `ID` = '{$post_ID}';");
}
function JD_capimagesize($content) {
//lists arrays of urls, widths and heights
$images=JD_GetImgUrlFromHtml($content,"url");
$widths=JD_GetImgUrlFromHtml($content,"width");
$heights=JD_GetImgUrlFromHtml($content,"height");
//cleans up width and height tags from images, and gets new urls from JD_capimage
$newimages=array();
foreach ($images as $key=>$url)
{
$images[$key]= 'src="'.$images[$key];
$newimages[$key]='src="'.JD_capimage($url);
$widths[$key]= 'width="'.$widths[$key].'"';
$heights[$key]= 'height="'.$heights[$key].'"';
}
//finishes clean up and replaces old url with new ones
$content=str_replace($images,$newimages,$content);
$content=str_replace($widths,'',$content);
$content=str_replace($heights,'',$content);
return $content;
}
function JD_capimage($url) {
$size = getimagesize($url);
$uploads=wp_upload_dir();
if ($size[0] > 500) {
$proportion = 500 / $size[0];
$height = floor($size[1] * $proportion);
$url = get_bloginfo('template_directory') . '/timthumb.php?src=' . $url . '&w=500&h='.$height.'&zc=1';
}
return $url;
}
function JD_savefile($newurl,$filename)
{
$content=@file_get_contents($newurl);
$fp=fopen($filename,"w+");
if (fwrite($fp,$content))
{
fclose($fp);
return $filename;
}
else
{
fclose($fp);
return false;
}
}
function JD_GetImgUrlFromHtml($text,$request)
{
preg_match_all("'<img[^>]*?>'si",$text,$n);
$oldimgsrc=array();
$oldwidth=array();
$oldheight=array();
foreach ($n as $m)
{
foreach ($m as $x)
{
preg_match('#src[[:space:]]*=[[:space:]]*[\'|"]?([[:alnum:]:@/._-]+[?]?[^\'|"]*)"?#ie',$x,$y);
$oldimgsrc[]=$y[1];
preg_match('#width[[:space:]]*=[[:space:]]*[\'|"]?([[:alnum:]:@/._-]+[?]?[^\'|"]*)"?#ie',$x,$y);
$oldwidth[]=$y[1];
preg_match('#height[[:space:]]*=[[:space:]]*[\'|"]?([[:alnum:]:@/._-]+[?]?[^\'|"]*)"?#ie',$x,$y);
$oldheight[]=$y[1];
}
}
switch ($request) {
case "src":
return $oldimgsrc;
break;
case "width":
return $oldwidth;
break;
case "height":
return $oldheight;
break;
}
return $oldimgsrc;
}












