moln4r Posted August 2, 2011 Share Posted August 2, 2011 Hello phpfreaks! i'm moln4r from hungary and registered here because i stucked with a php function in my wp template and want to ask you some help. I want to use phpimageresize to cache/resize and display my external thumbnails. this sounds easy even for a noob like me, i tested on the server in a standalone php file and it works fine, but when i tried to put the code inside my template loop i found this: <img src="<?php $values = get_post_custom_values("thumbs"); echo $values[0]; ?>" width="128" height="96" alt="<?php the_title(); ?>" /></a> ok wp getting the image from a custom field, and here we are... I can't mix the two code without syntax error. but i'm a noob so please somebody help me. this code call the phpimageresize function: <img src="<?=resize("http://anysite.com/anyimage.jpg" ,$settings)?>" /> and this is my default thumbnail: <img src="<?php $values = get_post_custom_values("thumbs"); echo $values[0]; ?>" width="128" height="96" alt="<?php the_title(); ?>" /></a> so it must be something like this, but my knowledge ends here: <img src="<?=resize("<?php $values = get_post_custom_values("thumbs"); echo $values[0]; ?>" ,$settings)?>" /> please help me how can I make this work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/ Share on other sites More sharing options...
Muddy_Funster Posted August 2, 2011 Share Posted August 2, 2011 assuming $ settings is what you want to use, and is the right format: does the following work? <img src="<?php $values = get_post_custom_values("thumbs"); echo $values[0]; echo $settings; echo 'alt ='.the_title(); ?>" /></a> Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1250740 Share on other sites More sharing options...
moln4r Posted August 2, 2011 Author Share Posted August 2, 2011 thanks! allmost work i made a little changes, take the alt attribute to the right place: <img src="<?php $values = get_post_custom_values("thumbs"); echo $values[0]; echo $settings; ?>" alt="<?php the_title(); ?>" /></a> and the output is now: <img src="http://anydomain.com/anyimage.jpgArray" alt="alt" /></a> the only problem now is the word Array is appear after the .jpg extension. edit: the thumb code is this: <div class="thumb"> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php $settings = array("w"=>100,"h"=>100,"crop"=>true); ?> <img src="<?php $values = get_post_custom_values("thumbs"); echo $values[0]; echo $settings; ?>" alt="<?php the_title(); ?>" /></a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1250770 Share on other sites More sharing options...
wildteen88 Posted August 2, 2011 Share Posted August 2, 2011 What does the function get_post_custom_values() return? What is the source code for that function? You first need to understand what that function does and what type of data it is returning before trying to merge the two functions together. Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1250775 Share on other sites More sharing options...
moln4r Posted August 2, 2011 Author Share Posted August 2, 2011 it returns the image url from the custom field named "thumbs" source: /** * Retrieve values for a custom post field. * * The parameters must not be considered optional. All of the post meta fields * will be retrieved and only the meta field key values returned. * * @since 1.2.0 * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values * * @param string $key Meta field key. * @param int $post_id Post ID * @return array Meta field values. */ function get_post_custom_values( $key = '', $post_id = 0 ) { if ( !$key ) return null; $custom = get_post_custom($post_id); return isset($custom[$key]) ? $custom[$key] : null; } the other function is phpimageresize source: <?php /* function by Wes Edling .. http://joedesigns.com feel free to use this in any project, i just ask for a credit in the source code. a link back to my site would be nice too. */ function resize($imagePath,$opts=null){ # start configuration $cacheFolder = 'cache/'; # path to your cache folder, must be writeable by web server $remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into $quality = 90; # image quality to use for ImageMagick (0 - 100) $cache_http_minutes = 20; # cache downloaded http images 20 minutes $path_to_convert = 'convert'; # this could be something like /usr/bin/convert or /opt/local/share/bin/convert ## you shouldn't need to configure anything else beyond this point $purl = parse_url($imagePath); $finfo = pathinfo($imagePath); $ext = $finfo['extension']; # check for remote image.. if(isset($purl['scheme']) && $purl['scheme'] == 'http'): # grab the image, and cache it so we have something to work with.. list($filename) = explode('?',$finfo['basename']); $local_filepath = $remoteFolder.$filename; $download_image = true; if(file_exists($local_filepath)): if(filemtime($local_filepath) < strtotime('+'.$cache_http_minutes.' minutes')): $download_image = false; endif; endif; if($download_image == true): $img = file_get_contents($imagePath); file_put_contents($local_filepath,$img); endif; $imagePath = $local_filepath; endif; if(file_exists($imagePath) == false): $imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath; if(file_exists($imagePath) == false): return 'image not found'; endif; endif; if(isset($opts['w'])): $w = $opts['w']; endif; if(isset($opts['h'])): $h = $opts['h']; endif; $filename = md5_file($imagePath); if(!empty($w) and !empty($h)): $newPath = $cacheFolder.$filename.'_w'.$w.'_h'.$h.(isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "").(isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "").'.'.$ext; elseif(!empty($w)): $newPath = $cacheFolder.$filename.'_w'.$w.'.'.$ext; elseif(!empty($h)): $newPath = $cacheFolder.$filename.'_h'.$h.'.'.$ext; else: return false; endif; $create = true; if(file_exists($newPath) == true): $create = false; $origFileTime = date("YmdHis",filemtime($imagePath)); $newFileTime = date("YmdHis",filemtime($newPath)); if($newFileTime < $origFileTime): $create = true; endif; endif; if($create == true): if(!empty($w) and !empty($h)): list($width,$height) = getimagesize($imagePath); $resize = $w; if($width > $height): $resize = $w; if(isset($opts['crop']) && $opts['crop'] == true): $resize = "x".$h; endif; else: $resize = "x".$h; if(isset($opts['crop']) && $opts['crop'] == true): $resize = $w; endif; endif; if(isset($opts['scale']) && $opts['scale'] == true): $cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath; else: $cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -size ".$w."x".$h." xc:".(isset($opts['canvas-color'])?$opts['canvas-color']:"transparent")." +swap -gravity center -composite -quality ".$quality." ".$newPath; endif; else: $cmd = $path_to_convert." ".$imagePath." -thumbnail ".(!empty($h) ? 'x':'').$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath; endif; $c = exec($cmd); endif; # return cache file path return str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1250783 Share on other sites More sharing options...
wildteen88 Posted August 2, 2011 Share Posted August 2, 2011 Then you probably need to use the resize function like so <div class="thumb"> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php $settings = array("w"=>100,"h"=>100,"crop"=>true); $values = get_post_custom_values("thumbs"); ?> <img src="<?php echo resize($values[0], $settings); ?>" alt="<?php the_title(); ?>" /> </a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1250788 Share on other sites More sharing options...
moln4r Posted August 2, 2011 Author Share Posted August 2, 2011 working thank you! but it returns the same thumbnail for all posts Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1250794 Share on other sites More sharing options...
moln4r Posted August 5, 2011 Author Share Posted August 5, 2011 not working because all posts use the same thumb.tried hard to found out what's happening but can't. !just realized the thumb is still the same 1 image for all posts, but the cache folder (phpimageresize) contains all resized images.thanks for any help. sry for the bump. <div class="thumb"> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php $settings = array("w"=>100,"h"=>100,"crop"=>true); $values = get_post_custom_values("thumbs"); ?> <img src="<?php echo resize($values[0], $settings); ?>" alt="<?php the_title(); ?>" /> </a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/243598-how-to-combine-these-2-functions/#findComment-1252377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.