Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add field type image

  • Loading branch information...
commit eb83e7f017d2fc03c1690aa11d70f89af123c15f 1 parent 0fa64bc
@hunk hunk authored
View
188 MF_thumb.php
@@ -0,0 +1,188 @@
+<?php
+/*
+ Magic Fields Thumb class
+
+ Paramters
+ ---------
+ w: width
+ h: height
+ zc: zoom crop (0 or 1)
+ q: quality (default is 75 and max is 100)
+*/
+class mfthumb{
+
+ function mfthumb(){
+ require_once(ABSPATH."/wp-admin/includes/image.php");
+ require_once(ABSPATH."/wp-includes/media.php");
+ }
+
+ /**
+ * This function is almost equal to the image_resize (native function of wordpress)
+ */
+ function image_resize( $file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90 ) {
+ $image = wp_load_image( $file );
+ if ( !is_resource( $image ) )
+ return new WP_Error('error_loading_image', $image);
+
+ $size = @getimagesize( $file );
+ if ( !$size )
+ return new WP_Error('invalid_image', __('Could not read image size'), $file);
+ list($orig_w, $orig_h, $orig_type) = $size;
+
+ $dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar);
+
+ if ( !$dims ){
+ $dims = array(0,0,0,0,$orig_w,$orig_h,$orig_w,$orig_h);
+ }
+ list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
+
+ $newimage = imagecreatetruecolor( $dst_w, $dst_h );
+ imagealphablending($newimage, false);
+ imagesavealpha($newimage, true);
+ $transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
+ imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent);
+ imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
+
+ // convert from full colors to index colors, like original PNG.
+ if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $image ) )
+ imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );
+
+ // we don't need the original in memory anymore
+ imagedestroy( $image );
+ $info = pathinfo($dest_path);
+ $dir = $info['dirname'];
+ $ext = $info['extension'];
+ $name = basename($dest_path, ".{$ext}");
+
+ $destfilename = "{$dir}/{$name}.{$ext}";
+
+ if ( IMAGETYPE_GIF == $orig_type ) {
+ if ( !imagegif( $newimage, $destfilename ) )
+ return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
+ } elseif ( IMAGETYPE_PNG == $orig_type ) {
+ if ( !imagepng( $newimage, $destfilename ) )
+ return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
+ } else {
+ // all other formats are converted to jpg
+ //Todo: add option for use progresive JPG
+ //imageinterlace($newimage, true); //Progressive JPG
+ if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
+ return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
+ }
+
+ imagedestroy( $newimage );
+
+ // Set correct file permissions
+ $stat = stat( dirname( $destfilename ));
+ $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
+ @ chmod( $destfilename, $perms );
+
+ return $destfilename;
+ }
+}
+
+
+/**
+ * Based in the image_resize_dimensions of wordpress
+ */
+function mf_image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false, $far = false, $iar = false) {
+
+
+ if ($orig_w <= 0 || $orig_h <= 0)
+ return false;
+ // at least one of dest_w or dest_h must be specific
+ if ($dest_w <= 0 && $dest_h <= 0)
+ return false;
+
+ if ( $crop ) {
+ // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
+ $aspect_ratio = $orig_w / $orig_h;
+ $new_w = min($dest_w, $orig_w);
+ $new_h = min($dest_h, $orig_h);
+
+ if ( !$new_w ) {
+ $new_w = intval($new_h * $aspect_ratio);
+ }
+
+ if ( !$new_h ) {
+ $new_h = intval($new_w / $aspect_ratio);
+ }
+
+ $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
+
+ $crop_w = round($new_w / $size_ratio);
+ $crop_h = round($new_h / $size_ratio);
+
+ $s_x = floor( ($orig_w - $crop_w) / 2 );
+ $s_y = floor( ($orig_h - $crop_h) / 2 );
+
+ } else {
+ // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
+ $crop_w = $dest_w;
+ $crop_h = $dest_h;
+
+ $s_x = 0;
+ $s_y = 0;
+
+ $new_w = $crop_w;
+ $new_h = $crop_h;
+ }
+
+ if( $far ) {
+ switch ( $far ) {
+ case 'L':
+ case 'TL':
+ case 'BL':
+ $s_x = 0;
+ $s_y = round(($dest_h - $origin_h) / 2);
+ break;
+ case 'R':
+ case 'TR':
+ case 'BR':
+ $s_x = round($dest_w - $origin_w);
+ $s_y = round(($dest_h - $origin_h) / 2);
+ break;
+ case 'T':
+ case 'TL':
+ case 'TR':
+ $s_x = round(($dest_w - $origin_w) / 2);
+ $s_y = 0;
+ break;
+ case 'B':
+ case 'BL':
+ case 'BR':
+ $s_x = round(($dest_w - $origin_w) / 2);
+ $s_y = round($dest_h - $origin_h);
+ break;
+ case 'C':
+ default:
+ $s_x = round(($dest_w - $origin_w) / 2);
+ $s_y = round(($dest_h - $origin_h) / 2);
+
+ }
+
+ }
+ if ( $iar ) {
+ //ignore aspect radio and resize the image
+ $crop_w = $orig_w;
+ $crop_h = $orig_h;
+
+ $s_x = 0;
+ $s_y = 0;
+
+ $new_w = ceil($orig_w * $dest_w / $orig_w);
+ $new_h = ceil($orig_h * $dest_h / $orig_h);
+
+ }
+
+ // if the resulting image would be the same size we don't want to resize it
+ if ( $new_w == $orig_w && $new_h == $orig_h )
+ return false;
+
+ // the return array matches the parameters to imagecopyresampled()
+ // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
+ return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
+
+}
+
+?>
View
7 admin/mf_custom_fields.php
@@ -681,4 +681,11 @@ public function display_field( $field, $group_index = 1, $field_index = 1 ) {
$output .= sprintf('<input %s type="text" name="%s" placeholder="%s" value="%s" />',$field['input_validate'], $field['input_name'], $field['label'], $field['input_value'] );
return $output;
}
+
+ public function upload($custom_field_id, $type = 'image',$callback = 'mf_callback_upload'){
+ $iframe_src = sprintf('%sadmin/mf_upload.php?input_name=%s&callback=%s&type=%s',MF_BASENAME,$custom_field_id,$callback,$type);
+ $out = sprintf('<iframe id="iframe_upload_%s" src="%s" ></iframe>',$custom_field_id,$iframe_src);
+
+ return $out;
+ }
}
View
151 admin/mf_upload.php
@@ -0,0 +1,151 @@
+<?php
+// use wp-load. Normally right here, but if it's not...
+if( file_exists('../../../../wp-load.php') )
+{
+ require_once('../../../../wp-load.php');
+ $loaded = true;
+} // ...then look over here
+elseif( file_exists('./mf-config.php') )
+{
+ include_once('./mf-config.php');
+ require_once(MF_WP_LOAD);
+ $loaded = true;
+}
+
+global $mf_domain;
+if( $loaded !== true ){
+ die('Could not load wp-load.php, edit/add mf-config.php and define MF_WP_LOAD to point to a valid wp-load file.');
+}
+
+/**
+* Check the mime type of the file for
+* avoid upload any dangerous file.
+*
+* @param string $mime is the type of file can be "image","audio" or "file"
+* @param string $file_type is the mimetype of the field
+*/
+function valid_mime($mime,$file_type){
+ $imagesExts = array(
+ 'image/gif',
+ 'image/jpeg',
+ 'image/pjpeg',
+ 'image/png',
+ 'image/x-png'
+ );
+ $audioExts = array(
+ 'audio/mpeg',
+ 'audio/mpg',
+ 'audio/x-wav',
+ 'audio/mp3'
+ );
+
+ if($file_type == "image"){
+ if(in_array($mime,$imagesExts)){
+ return true;
+ }
+ }elseif($file_type == "audio"){
+ if(in_array($mime,$audioExts)){
+ return true;
+ }
+ }else{
+ //TODO: here users should be set what mime types
+ //are safety for the "files" type of field
+ return true;
+ }
+ return false;
+}
+
+?>
+<html>
+<head>
+<?php
+if( isset($_POST['fileframe']) ){
+ $resp = array('error' => true, 'field_id' => $_POST['input_name'] ,'msg' => __("Upload Unsuccessful",$mf_domain) );
+ if (isset($_FILES['file']) && (!empty($_FILES['file']['tmp_name']))){
+
+ if ($_FILES['file']['error'] == UPLOAD_ERR_OK){
+ if(valid_mime($_FILES['file']['type'],$_POST['type'])){
+ $special_chars = array(' ','`','"','\'','\\','/'," ","#","$","%","^","&","*","!","~","","\"","","'","=","?","/","[","]","(",")","|","<",">",";","\\",",","+","-");
+ $filename = str_replace($special_chars,'',$_FILES['file']['name']);
+ $filename = time() . $filename;
+
+ @move_uploaded_file( $_FILES['file']['tmp_name'], MF_FILES_DIR . $filename );
+ @chmod(MF_FILES_DIR . $filename, 0644);
+ $info = pathinfo(MF_FILES_DIR . $filename);
+
+ $resp = array(
+ 'error' => false,
+ 'name' => $filename,
+ 'ext' => $info['extension'],
+ 'field_id' => $_POST['input_name'],
+ 'file_path' => MF_FILES_DIR . $filename,
+ 'file_url' => MF_FILES_URL . $filename,
+ 'phpthumb' => PHPTHUMB,
+ 'msg' => __("Successful upload",$mf_domain)
+ );
+
+ }else{
+ $resp['msg'] = __("Failed to upload the file!",$mf_domain);
+ }
+ }elseif( $_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE ){
+ $resp['msg'] = __('The uploaded file exceeds the maximum upload limit!',$mf_domain);
+ }else{
+ $resp['msg'] = __("Upload Unsuccessful",$mf_domain);
+ }
+ }
+?>
+
+ <script type="text/javascript" charset="utf-8">
+ var mf_par = window.parent;
+ var mf_js = <?php echo json_encode($resp); ?>;
+ mf_par.<?php echo $_POST['callback']; ?>(mf_js);
+
+ var par = window.parent.document;
+ var iframe = par.getElementById('iframe_upload_<?php echo $_POST["input_name"]?>');
+ iframe.style.display="";
+ </script>
+<?php } ?>
+<style>
+body { padding: 0px; margin: 0px; vertical-align:top; background: transparent; }
+</style>
+<script language="javascript">
+function upload(){
+ // hide old iframe
+ var par = window.parent.document;
+ var iframe = par.getElementById('iframe_upload_<?php echo $_GET["input_name"]?>');
+ iframe.style.display="none";
+
+ par.getElementById("response-<?php echo $_GET['input_name'];?>").style.display = "block";
+ par.getElementById("response-<?php echo $_GET['input_name'];?>").innerHTML = "Transferring ";
+ setTimeout("transferring(0)",1000);
+ // send
+ document.iform.submit();
+}
+function transferring(dots){
+
+ newString = "Transferring ";
+ for (var x=1; x<=dots; x++) {
+ newString = newString + ".";
+ }
+
+ var par = window.parent.document;
+ // update progress
+ if (par.getElementById("response-<?php echo $_GET['input_name'];?>").innerHTML.substring(0,5) != "Trans") return;
+ par.getElementById("response-<?php echo $_GET['input_name'];?>").innerHTML = newString;
+ if (dots == 4) dots = 0; else dots = dots + 1;
+ setTimeout("transferring("+dots+")",1000) ;
+
+}
+</script>
+</head>
+<body>
+<form name="iform" action="" method="post" enctype="multipart/form-data">
+ File:
+ <input id="file" type="file" name="file" onchange="upload()" />
+ <input type="hidden" name="input_name" value="<?php echo $_GET["input_name"]?>" />
+ <input type="hidden" name="callback" value="<?php echo $_GET["callback"]?>" />
+ <input type="hidden" name="fileframe" value="true" />
+ <input type="hidden" name="type" value="<?php echo $_GET["type"]?>" />
+</form>
+</body>
+</html>
View
60 css/mf_field_base.css
@@ -219,4 +219,62 @@ textarea.mf_editor { width: 99%; padding: 8px;}
height:18px;
margin:5px 5px 0 0;
padding:4px 5px 2px;
-}
+}
+
+/* image upload */
+.image_layout{ padding-left: 10px; }
+.mf-field-ui .image_photo {
+ border: 1px solid #EAEAEA;
+ background: #FFF;
+ width: 158px !important;
+ margin-top: 2px;
+}
+.mf-field-ui .image_photo img {
+ margin: 4px;
+}
+.image_photo .photo_edit_link {
+ line-height: 18px;
+ border-top: 1px solid #EAEAEA;
+ width: 158px;
+ text-align: center;
+}
+
+.image_photo .photo_edit_link a {
+ text-decoration: none;
+ font-weight: bold;
+ color: #21759B;
+}
+.image_photo .photo_edit_link a.remove{
+ color: #BB3939;
+}
+.image_layout .image_photo {
+ width: 150px;
+ float: left;
+ margin-right: 14px;
+}
+.image_layout .image_wrap {
+ min-height: 78px;
+ text-align: center;
+}
+.image_layout .image_input {
+ float: left;
+ margin-top: 10px;
+ /*width: 520px;*/
+}
+
+.mf-upload-success,
+.mf-upload-error {
+ background-repeat: no-repeat;
+ display: block;
+ padding-left: 20px;
+ display: block;
+ line-height: 16px;
+}
+.mf-upload-success {
+ color: #11A122;
+ background-image: url(../images/mf-upload-success.png);
+}
+.mf-upload-error {
+color: #9E1111;
+background-image: url(../images/mf-upload-error.png);
+}
View
46 field_types/image_field/image_field.js
@@ -0,0 +1,46 @@
+function mf_image_callback_upload(data){
+
+ if(data.error == false){
+ //como aun tiene jquery 1.4 aun no tiene prop
+ var image_thumb = data.phpthumb;
+ image_thumb += '?&w=150&h=120&zc=1&src=';
+ image_thumb += data.file_url;
+
+ jQuery('#img_thumb_'+data.field_id).attr('src',image_thumb);
+ jQuery('#edit-'+data.field_id).attr('href',data.file_url);
+ jQuery('#'+data.field_id).val(data.name);
+
+ var success_resp = '<span class="mf-upload-success" >'+data.msg+'</span>';
+ jQuery('#response-'+data.field_id).html(success_resp).show();
+ jQuery('#photo_edit_link_'+data.field_id).show();
+
+ setTimeout("remove_resp('#response-"+data.field_id+"')",5000);
+
+ }else{
+ //show errors
+ var error_resp = '<span class="mf-upload-error" >'+data.msg+'</span>';
+ jQuery('#response-'+data.field_id).html(error_resp).show();
+ setTimeout("remove_resp('#response-"+data.field_id+"')",5000);
+ }
+
+}
+
+function remove_resp(field_id){
+ jQuery(field_id).fadeOut('slow', function(){
+ jQuery(this).empty();
+ });
+}
+
+jQuery('.remove_photo').live('click', function(){
+ if(confirm("Are you sure?")){
+ var pattern = /remove\-(.+)/i;
+ var id = jQuery(this).attr('id');
+ id = pattern.exec(id);
+ id = id[1];
+
+ //todo añadir al arreglo de estan los files a borrar
+ jQuery('#'+id).val('');
+ jQuery('#photo_edit_link_'+id).hide();
+ jQuery("#img_thumb_"+id).attr("src",mf_js.mf_url+"images/noimage.jpg");
+ }
+});
View
44 field_types/image_field/image_field.php
@@ -9,7 +9,16 @@ class image_field extends mf_custom_fields {
public $allow_multiple = TRUE;
public $has_properties = TRUE;
-
+
+
+ function get_properties() {
+ return array(
+ 'js' => TRUE,
+ 'js_dependencies' => array(),
+ 'css' => FALSE
+ );
+ }
+
public function _update_description(){
global $mf_domain;
$this->description = __("Simple image input",$mf_domain);
@@ -69,5 +78,38 @@ public function _options(){
return $data;
}
+
+ public function display_field( $field, $group_index = 1, $field_index = 1){
+ global $mf_domain;
+
+ $field_style = '';
+ $imageThumbID = "img_thumb_".$field['input_id'];
+ if(!$field['input_value']){
+ $value = sprintf('%simages/noimage.jpg',MF_URL);
+ $field_style = 'style="display:none;"';
+ }else{
+ $value = sprintf("%s?src=%s%s&w=150&h=120&zc=1",PHPTHUMB,MF_FILES_URL,$field['input_value']);
+ }
+
+ $value = sprintf('<img src="%s" id="%s" />',$value,$imageThumbID);
+
+ $out = '<div class="image_layout">';
+ $out .= '<div class="image_photo"><div class="image_wrap">';
+ $out .= $value;
+ $out .= '</div>';
+ $out .= sprintf('<div id="photo_edit_link_%s" %s class="photo_edit_link">',$field['input_id'],$field_style);
+ $out .= sprintf('<a href="%s" target="_blank" id="edit-%s" >%s</a> | ',MF_FILES_URL.$field['input_value'],$field['input_id'],__('View',$mf_domain));
+ $out .= sprintf('<a href="#remove" class="remove remove_photo" id="remove-%s" >%s</a>',$field['input_id'],__('Delete',$mf_domain));
+ $out .= '</div>';
+ $out .='</div>';
+ $out .= '<div class="image_input">';
+ $out .= '<div class="mf_custom_field">';
+ $out .= sprintf('<div id="response-%s" style="display:none;" ></div>',$field['input_id']);
+ $out .= sprintf('<input type="hidden" value="%s" name="%s" id="%s" >',$field['input_value'],$field['input_name'],$field['input_id']);
+ $out .= $this->upload($field['input_id'],'image','mf_image_callback_upload');
+ $out .= '</div></div>';
+ $out .= '</div>';
+ return $out;
+ }
}
View
BIN  images/mf-upload-error.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/mf-upload-success.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/noimage.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
3  main.php
@@ -63,6 +63,9 @@ function __autoload( $name ) {
if( is_admin() ) {
+ //check folders
+ add_action('admin_notices', array('mf_install', 'folders'));
+
//add common function
require_once(MF_PATH.'/mf_common.php');
View
2  mf_common.php
@@ -103,4 +103,4 @@ function getMemoryUsage($message="", $echo=1)
$res .= "[MEM_USED: $mem_used MB ($percentual%) - Peak: $mem_peak MB]\n";
if($echo) echo $res;
else return $res;
-}
+}
View
21 mf_constants.php
@@ -1,11 +1,30 @@
<?php
+
+if ( !defined('DS') ){
+ if (strpos(php_uname('s'), 'Win') !== false )
+ define('DS', '\\');
+ else
+ define('DS', '/');
+}
+
//useful for get quickly the path for images/javascript files and css files
//return something like: http://wordpress.local/wp-content/plugins/Magic-Fields/
define('MF_BASENAME',plugins_url().'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__)));
-
+define('MF_URL',MF_BASENAME);
//return something like: /Users/user/sites/wordpres/wp-content/plugins/Magic-Fields
define("MF_PATH", dirname(__FILE__));
+define("PHPTHUMB",MF_URL."thirdparty/phpthumb/phpThumb.php");
+
+define('MF_FILES_NAME','files_mf');
+define('MF_CACHE_NAME','cache');
+//ToDo: falta probar que en MU este colocando el path correcto
+define('MF_FILES_DIR', WP_CONTENT_DIR.DS.MF_FILES_NAME.DS);
+define('MF_FILES_URL', WP_CONTENT_URL.'/'.MF_FILES_NAME.'/');
+
+define('MF_CACHE_DIR', MF_FILES_DIR.MF_CACHE_NAME.DS);
+define('MF_CACHE_URL', MF_FILES_URL.MF_CACHE_NAME.'/');
+
global $supports,$wpdb;
$supports = array(
View
34 mf_install.php
@@ -90,4 +90,38 @@ function install () {
dbDelta($sql);
}
}
+
+ public function folders(){
+ global $mf_domain;
+
+ $dir_list = "";
+ $dir_list2 = "";
+
+ wp_mkdir_p(MF_FILES_DIR);
+ wp_mkdir_p(MF_CACHE_DIR);
+
+ if (!is_dir(MF_CACHE_DIR)){
+ $dir_list2.= "<li>".MF_CACHE_DIR . "</li>";
+ }elseif (!is_writable(MF_CACHE_DIR)){
+ $dir_list.= "<li>".MF_CACHE_DIR . "</li>";
+ }
+
+ if (!is_dir(MF_FILES_DIR)){
+ $dir_list2.= "<li>".MF_FILES_DIR . "</li>";
+ }elseif (!is_writable(MF_FILES_DIR)){
+ $dir_list.= "<li>".MF_FILES_DIR . "</li>";
+ }
+
+ if ($dir_list2 != ""){
+ echo "<div id='magic-fields-install-error-message' class='error'><p><strong>".__('Magic Fields is not ready yet.', $mf_domain)."</strong> ".__('must create the following folders (and must chmod 777):', $mf_domain)."</p><ul>";
+ echo $dir_list2;
+ echo "</ul></div>";
+ }
+ if ($dir_list != ""){
+ echo "<div id='magic-fields-install-error-message-2' class='error'><p><strong>".__('Magic Fields is not ready yet.', $mf_domain)."</strong> ".__('The following folders must be writable (usually chmod 777 is neccesary):', $mf_domain)."</p><ul>";
+ echo $dir_list;
+ echo "</ul></div>";
+ }
+
+ }
}
View
110 thirdparty/phpthumb/phpThumb.php
@@ -0,0 +1,110 @@
+<?php
+ob_start();
+//use wp-load. Normally right here, but if it's not...
+if( file_exists('../../../../../wp-load.php')){
+ require_once('../../../../../wp-load.php');
+ $loaded = true;
+} elseif( file_exists( dirname(__FILE__).'/../../mf-config.php')){
+ include_once(dirname(__FILE__).'/../../mf-config.php');
+
+ include_once('./mf-config.php');
+ require_once(MF_WP_LOAD);
+ $loaded = true;
+}
+
+if($loaded !== true){
+ die('Could not load wp-load.php, edit/add mf-config.php and define MF_WP_LOAD to point to a valid wp-load file');
+}
+ob_end_clean();
+
+$MFthumb = MF_PATH.'/MF_thumb.php';
+
+require_once($MFthumb);
+
+//Default Values
+$default = array(
+ 'iar' => 0,
+ 'far' => 0,
+ 'zc' => 1,
+ 'q' => 95,
+ 'w' => 0,
+ 'h' => 0,
+ 'src' => ''
+);
+
+//getting the name of the image
+preg_match('/\/wp-content\/([0-9\_a-z\/\-\.]+\.(jpg|jpeg|png|gif))/i',$_GET['src'],$match);
+$image_name_clean = $match[1];
+$extension = $match[2];
+
+//is wp mu o wp network
+if(isset($current_blog)){
+ $image_name_clean = preg_replace('/blogs.dir\/(\d+)\//','',$image_name_clean);
+}
+//Getting the original size of the image
+
+$file = WP_CONTENT_DIR.DS.$image_name_clean;
+
+if(file_exists($file) && (empty($_GET['w']) || empty($_GET['h']))){
+ $size = @getimagesize($file);
+ $default['w'] = $size[0];
+ $default['h'] = $size[1];
+}
+//TODO: sanitize the variables
+$params = array();
+foreach($_GET as $key => $value){
+ if(in_array($key,array('zc','w','h','q','src','far','iar'))){
+ $params[$key] = $value;
+ }
+}
+
+$params = array_merge($default,$params);
+$md5_params = md5("w=".$params['w']."&h=".$params['h']."&q=".$params['q']."&zc=".$params['zc']."&far=".$params['far']."&iar=".$params['iar']);
+
+//The file must be "jpg" or "png" or "gif"
+if(!in_array(strtolower($extension),array('jpg','jpeg','png','gif'))){
+ return false;
+}
+
+//
+$image_sin = preg_split('/\//',$image_name_clean);
+$new_image_clean = $image_sin[count($image_sin)-1];
+//name with a png extension
+$image_name = $md5_params."_".$new_image_clean;
+//this code can be refactored
+if(file_exists(MF_CACHE_DIR.$image_name)){
+ //Displaying the image
+ $size = getimagesize(MF_CACHE_DIR.$image_name);
+ $handle = fopen(MF_CACHE_DIR.$image_name, "rb");
+ $contents = NULL;
+ while (!feof($handle)) {
+ $contents .= fread($handle, 1024);
+ }
+ fclose($handle);
+
+ header("Cache-Control: public");
+ header ("Content-type: image/".$extension);
+ header("Content-Disposition: inline; filename=\"".MF_CACHE_DIR.$image_name."\"");
+ header('Content-Length: ' . filesize(MF_CACHE_DIR.$image_name));
+ echo $contents;
+}else{
+ //generating the image
+ $thumb = new mfthumb();
+ $thumb_path = $thumb->image_resize($file,$params['w'],$params['h'],$params['zc'],$params['far'],$params['iar'],MF_CACHE_DIR.$image_name);
+ //Displaying the image
+ if(file_exists($thumb_path)){
+ $size = getimagesize($thumb_path);
+ $handle = fopen($thumb_path, "rb");
+ $contents = NULL;
+ while (!feof($handle)) {
+ $contents .= fread($handle, filesize($thumb_path));
+ }
+ fclose($handle);
+
+ header("Cache-Control: public");
+ header ("Content-type: image/".$extension);
+ header("Content-Disposition: inline; filename=\"".$thumb_path."\"");
+ header('Content-Length: ' . filesize($thumb_path));
+ echo $contents;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.