Skip to content

Instantly share code, notes, and snippets.

@hunk
Created June 2, 2010 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hunk/421955 to your computer and use it in GitHub Desktop.
Save hunk/421955 to your computer and use it in GitHub Desktop.
<?php
// add phpthumb
function get_image_p ($fieldName, $groupIndex=1, $fieldIndex=1,$tag_img=1,$post_id=NULL,$override_params=NULL) {
return create_image_p(array(
'fieldName' => $fieldName,
'groupIndex' => $groupIndex,
'fieldIndex' => $fieldIndex,
'param' => $override_params,
'post_id' => $post_id,
'tag_img' => (boolean) $tag_img
));
}
// generate image
function gen_image_p ($fieldName, $groupIndex=1, $fieldIndex=1,$param=NULL,$attr=NULL,$post_id=NULL) {
return create_image_p(array(
'fieldName' => $fieldName,
'groupIndex' => $groupIndex,
'fieldIndex' => $fieldIndex,
'param' => $param,
'attr' => $attr,
'post_id' => $post_id
));
}
function create_image_p($options)
{
require_once("RCCWP_CustomField.php");
global $post;
// establish the default values, then override them with
// whatever the user has passed in
$options = array_merge(array(
// the default options
'fieldName' => '',
'groupIndex' => 1,
'fieldIndex' => 1,
'param' => NULL,
'attr' => NULL,
'post_id' => NULL,
'tag_img' => true
), (array) $options);
// finally extract them into variables for this function
extract($options);
// check for a specified post id, or see if the $post global has one
if($post_id){
$post_id = $post_id;
}elseif(isset($post->ID)){
$post_id = $post->ID;
} else {
return false;
}
// basic check
if(empty($fieldName)) return FALSE;
$field = RCCWP_CustomField::GetDataField($fieldName,$groupIndex, $fieldIndex,$post_id);
if(!$field) return FALSE;
$fieldType = $field['type'];
$fieldID = $field['id'];
$fieldCSS = $field['CSS'];
$fieldObject = $field['properties'];
$fieldValue = $field['meta_value'];
if(empty($fieldValue)) return "";
// override the default phpthumb parameters if needed
// works with both strings and arrays
if(!empty($param)) {
if(is_array($param)){
$p = array();
foreach($param as $k => $v){
$p[] = $k."=".$v;
}
$fieldObject['params'] = implode('&', $p);
} else {
$fieldObject['params'] = $param;
}
}
// remove the ? on the params if it happened to be there
if (substr($fieldObject['params'], 0, 1) == "?"){
$fieldObject['params'] = substr($fieldObject['params'], 1);
}
// check if exist params, if not exist params, return original image
if (empty($fieldObject['params']) && (FALSE === strstr($fieldValue, "&"))){
$fieldValue = MF_FILES_URI.$fieldValue;
}else{
//check if exist thumb image, if exist return thumb image
$md5_params = md5($fieldObject['params']);
if (file_exists(MF_CACHE_DIR.'pth_'.$md5_params."_".$fieldValue)) {
$fieldValue = MF_CACHE_URI.'pth_'.$md5_params."_".$fieldValue;
}else{
//generate thumb
include_once(dirname(__FILE__)."/thirdparty/phpthumb_old/phpthumb.class.php");
$phpThumb = new phpThumb();
$phpThumb->setSourceFilename(MF_FILES_PATH.$fieldValue);
$create_md5_filename = 'pth_'.$md5_params."_".$fieldValue;
$output_filename = MF_CACHE_DIR.$create_md5_filename;
$final_filename = MF_CACHE_URI.$create_md5_filename;
$params_image = explode("&",$fieldObject['params']);
foreach($params_image as $param){
if($param){
$p_image=explode("=",$param);
$phpThumb->setParameter($p_image[0], $p_image[1]);
}
}
if ($phpThumb->GenerateThumbnail()) {
if ($phpThumb->RenderToFile($output_filename)) {
$fieldValue = $final_filename;
}
}
}
}
if($tag_img){
// make sure the attributes are an array
if( !is_array($attr) ) $attr = (array) $attr;
// we're generating an image tag, but there MAY be a default class.
// if one was defined, however, override it
if( !isset($attr['class']) && !empty($fieldCSS) )
$attr['class'] = $fieldCSS;
// ok, put it together now
if(count($attr)){
foreach($attr as $k => $v){
$add_attr .= $k."='".$v."' ";
}
$finalString = "<img src='".$fieldValue."' ".$add_attr." />";
}else{
$finalString = "<img src='".$fieldValue."' />";
}
}else{
$finalString = $fieldValue;
}
return $finalString;
}
define("PHPTHUMB_OLD",MF_URI."thirdparty/phpthumb_old/phpThumb.php");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment