WordPress.org

Forums

fix to make it work with WPG2 plugin & other php-generated images calls. (4 posts)

  1. grosbouff
    Member
    Posted 7 years ago #

    Hello !
    Maybe this isn't perfect yet, but I made an update of the tb_post_thumb function of Post thumb.

    Using this, the plugin works with images insert into posts via URLS : for example; this
    http://www.monsite.com/main.php?g2_view=core.DownloadItem&g2_itemId=38&g2_GALLERYSID=626998fb440fbf8ab52fdde92af223bb

    would not work with the original function but works with this fix.

    function tb_post_thumb($generate=false, $alt_text='', $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0) {
    
    	global $post;
    
    	$settings = get_option('post_thumbnail_settings');
    
    	// dir to save thumbnail to
    
    	$save_dir = $dest_path['dirname']."/{$settings['folder_name']}"; //GALLERY2 MOD : this has been moved from above
    
    	// find an image from your domain
    
    	if (preg_match('/<img (.*?)src="https?:\/\/(www\.)?'.str_replace('/','\/',$settings['domain_name']).'\/(.*?)"/i',$post->post_content,$matches)) {
    
    		// put matches into recognizable vars
    
    		// fix later, assumes document root will match url structure
    
    		$the_image = $settings['base_path'] . '/' . $matches[3];
    
    		///GALLERY2 MOD - IMGS TAGS
    
    		$the_image_url = $settings['full_domain_name'] . '/' . $matches[3];
    
    		$the_image_url = str_replace ('&amp;','&', $the_image_url);
    
    		$save_path = $settings['base_path'].$dest_path['dirname']."/{$settings['folder_name']}";
    
    		$g2_pattern = "g2_itemId=";
    
    		$g2_image = explode($g2_pattern, $the_image);
    
    		if(!empty($g2_image[1])) { //G2 MOD
    
    			$g2_id = explode('&', $g2_image[1]);
    
    			$g2_id = $g2_id[0];
    
    			$the_image = htmlspecialchars_decode($the_image);
    
    			$ch = curl_init(); // create cURL handle (ch)
    
    			if (!$ch) {
    
    			    die("Couldn't initialize a cURL handle");
    
    			}
    
    			// set some cURL options
    
    			$ret = curl_setopt($ch, CURLOPT_URL,            $the_image_url);
    
    			$ret = curl_setopt($ch, CURLOPT_HEADER,         1);
    
    			$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    
    			$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    			$ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);
    
    			// execute
    
    			$ret = curl_exec($ch);
    
    			$content_type = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );
    
    			curl_close($ch);
    
    			if ($content_type == 'image/png') {
    
    				$new_ext = '.png';
    
    			}elseif ($content_type == 'image/gif') {
    
    				$new_ext = '.gif';
    
    			}elseif ($content_type == 'image/jpeg') {
    
    				$new_ext = '.jpg';
    
    			}elseif ($content_type == 'image/jpeg') {
    
    				$new_ext = '.jpg';
    
    			}elseif ($content_type == 'image/bmp') {
    
    				$new_ext = '.bmp';
    
    			}elseif ($content_type == 'image/tiff') {
    
    				$new_ext = '.tif';
    
    			}
    
    			if ($new_ext) { //si le fichier est une image
    
    				$save_url = $settings['full_domain_name'].$dest_path['dirname']."/{$settings['folder_name']}";
    
    				$the_new_path = $save_path.'/wpg2-'.$g2_id.$new_ext;
    
    				$the_new_url = $save_url.'/wpg2-'.$g2_id.$new_ext;
    
    				$the_new_thumb_url = $save_url.'/.wpg2-'.$g2_id.$new_ext;
    
    				$the_new_thumb_path = $save_path.'/.wpg2-'.$g2_id.$new_ext;
    
    				$image = $the_new_thumb_path;
    
    			}
    
    		}
    
    		// check if image exists on server
    
    		// if doesn't exist, can't do anything so return default image
    
    		if ((!file_exists($the_image)) && (!$the_new_thumb_path) ) {
    
    			return tb_post_thumb_gen_image	(
    
    												str_replace($settings['full_domain_name'],$settings['base_path'],$settings['default_image']),
    
    												$settings['default_image'],
    
    												$alt_text,
    
    												$generate
    
    											);
    
    		} elseif ($the_new_thumb_path) {
    
    			if (!file_exists($the_new_thumb_path)) {
    
    				if (copy($the_image_url, $the_new_path)){
    
    					chmod ($the_new_path, 0777);
    
    					$the_image = $the_new_path;
    
    				} else {
    
    					unset($the_new_path);
    
    					echo "probleme lors de la copie";
    
    				}
    
    			} else {
    
    				return tb_post_thumb_gen_image	(
    
    													$the_new_thumb_path,
    
    													$the_new_thumb_url,
    
    													$alt_text,
    
    													$generate
    
    												);
    
    			}		
    
    		}
    
    		$dest_path = pathinfo($the_image);
    
    		// name to save to
    
    		if ($settings['append'] == 'true') {
    
    			$filename = substr($dest_path['basename'], 0, strrpos($dest_path['basename'], "."));
    
    			$rename_to = $filename.$settings['append_text'].'.'.$dest_path['extension'];
    
    		}
    
    		else $rename_to = $settings['append_text'].$dest_path['basename'];
    
    		// check if file already exists
    
    		// return location if does
    
    		if (file_exists($save_path.'/'.$rename_to)) {
    
    			return tb_post_thumb_gen_image	(
    
    												$save_path.'/'.$rename_to,
    
    												$save_dir.'/'.$rename_to,
    
    												$alt_text,
    
    												$generate
    
    											);
    
    		}
    
    		// sticky bit?
    
    		if (!is_dir($save_path)) mkdir($save_path,02775);
    
    		// manipulate thumbnails
    
    		$thumb = new ImageEditor($dest_path['basename'],$dest_path['dirname'].'/');
    
    		$thumb->resize($settings['resize_width'], $settings['resize_height'], $settings['keep_ratio']);
    
    		if ($settings['crop_exact'] == 'true' || ($crop_x != 0 && $crop_y != 0)) {
    
    			if ($crop_x != 0 && $crop_y != 0) {
    
    				$settings['resize_width'] = $crop_x;
    
    				$settings['resize_height'] = $crop_y;
    
    			}
    
    			if ($thumb->x > $settings['resize_width' || $thumb->y > $settings['resize_height']]) {
    
    				$thumb->crop(	(int)(($thumb->x - $settings['resize_width']) / 2),
    
    								0,
    
    								$settings['resize_width'],
    
    								$settings['resize_height']
    
    							);
    
    			}
    
    		}
    
    		$thumb->outputFile($save_path."/".$rename_to, "");
    
    		if ($the_new_path) { //G2 MOD
    
    			unlink($the_new_path);
    
    		}
    
    		return tb_post_thumb_gen_image	(
    
    												$save_path.'/'.$rename_to,
    
    												$save_dir.'/'.$rename_to,
    
    												$alt_text,
    
    												$generate
    
    											);
    
    	} else {
    
    		if (!empty($settings['video_regex']) && tb_post_thumb_check_video($settings['video_regex'])) {
    
    				$settings['default_image'] = $settings['video_default'];
    
    		}
    
    		return tb_post_thumb_gen_image	(
    
    											str_replace($settings['full_domain_name'],$settings['base_path'],$settings['default_image']),
    
    											$settings['default_image'],
    
    											$alt_text,
    
    											$generate
    
    										);
    
    	}
    
    }
  2. grosbouff
    Member
    Posted 7 years ago #

    An update :

    function tb_post_thumb($generate=false, $alt_text='', $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0) {
    
    	global $post;
    
    	$settings = get_option('post_thumbnail_settings');
    
    	// dir to save thumbnail to
    
    	$save_dir = $dest_path['dirname']."/{$settings['folder_name']}"; //GALLERY2 MOD : this has been moved from above
    
    	// find an image from your domain
    
    	if (preg_match('/<img (.*?)src="https?:\/\/(www\.)?'.str_replace('/','\/',$settings['domain_name']).'\/(.*?)"/i',$post->post_content,$matches)) {
    
    		// put matches into recognizable vars
    
    		// fix later, assumes document root will match url structure
    
    		$the_image = $settings['base_path'] . '/' . $matches[3];
    
    		///GALLERY2 MOD - IMGS TAGS
    
    		$the_image_url = $settings['full_domain_name'] . '/' . $matches[3];
    
    		$the_image_url = str_replace ('&amp;','&', $the_image_url);
    
    		$save_path = $settings['base_path'].$dest_path['dirname']."/{$settings['folder_name']}";
    
    		$g2_pattern = "g2_itemId=";
    
    		$g2_image = explode($g2_pattern, $the_image);
    
    		if(!empty($g2_image[1])) { //G2 MOD
    
    			$g2_id = explode('&', $g2_image[1]);
    
    			$g2_id = $g2_id[0];
    
    			$the_image = htmlspecialchars_decode($the_image);
    
    			$ch = curl_init(); // create cURL handle (ch)
    
    			if (!$ch) {
    
    			    die("Couldn't initialize a cURL handle");
    
    			}
    
    			// set some cURL options
    
    			$ret = curl_setopt($ch, CURLOPT_URL,            $the_image_url);
    
    			$ret = curl_setopt($ch, CURLOPT_HEADER,         1);
    
    			$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    
    			$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    			$ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);
    
    			// execute
    
    			$ret = curl_exec($ch);
    
    			$content_type = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );
    
    			curl_close($ch);
    
    			if (preg_match("/image\/png/", $content_type)) {
    
    				$new_ext = '.png';
    
    			}elseif (preg_match("/image\/gif/", $content_type)) {
    
    				$new_ext = '.gif';
    
    			}elseif ( (preg_match("/image\/jpg/", $content_type)) || (preg_match("/image\/jpeg/", $content_type)) ) {
    
    				$new_ext = '.jpg';
    
    			}elseif (preg_match("/image\/bmp/", $content_type)) {
    
    				$new_ext = '.bmp';
    
    			}elseif (preg_match("/image\/tiff/", $content_type)) {
    
    				$new_ext = '.tif';
    
    			}			
    
    			if ($new_ext) { //si le fichier est une image
    
    				$save_url = $settings['full_domain_name'].$dest_path['dirname']."/{$settings['folder_name']}";
    
    				$the_new_path = $save_path.'/wpg2-'.$g2_id.$new_ext;
    
    				$the_new_url = $save_url.'/wpg2-'.$g2_id.$new_ext;
    
    				$the_new_thumb_url = $save_url.'/.wpg2-'.$g2_id.$new_ext;
    
    				$the_new_thumb_path = $save_path.'/.wpg2-'.$g2_id.$new_ext;
    
    				$the_image = $the_new_thumb_path;
    
    			}
    
    		}
    
    		if ((!file_exists($the_image)) && (!$the_new_thumb_path) ) {
    
    			return tb_post_thumb_gen_image	(
    
    												str_replace($settings['full_domain_name'],$settings['base_path'],$settings['default_image']),
    
    												$settings['default_image'],
    
    												$alt_text,
    
    												$generate
    
    											);
    
    		} elseif ($the_new_thumb_path) {
    
    			if (!file_exists($the_new_thumb_path)) {
    
    				if (copy($the_image_url, $the_new_path)){
    
    					chmod ($the_new_path, 0777);
    
    					$the_image = $the_new_path;
    
    				} else {
    
    					unset($the_new_path);
    
    					echo "probleme lors de la copie";
    
    				}
    
    			} else {
    
    				return tb_post_thumb_gen_image	(
    
    													$the_new_thumb_path,
    
    													$the_new_thumb_url,
    
    													$alt_text,
    
    													$generate
    
    												);
    
    			}		
    
    		}
    
    		$dest_path = pathinfo($the_image);
    
    		// name to save to
    
    		if ($settings['append'] == 'true') {
    
    			$filename = substr($dest_path['basename'], 0, strrpos($dest_path['basename'], "."));
    
    			$rename_to = $filename.$settings['append_text'].'.'.$dest_path['extension'];
    
    		}
    
    		else $rename_to = $settings['append_text'].$dest_path['basename'];
    
    		// check if file already exists
    
    		// return location if does
    
    		if (file_exists($save_path.'/'.$rename_to)) {
    
    			return tb_post_thumb_gen_image	(
    
    												$save_path.'/'.$rename_to,
    
    												$save_dir.'/'.$rename_to,
    
    												$alt_text,
    
    												$generate
    
    											);
    
    		}
    
    		// sticky bit?
    
    		if (!is_dir($save_path)) mkdir($save_path,02775);
    
    		// manipulate thumbnails
    
    		$thumb = new ImageEditor($dest_path['basename'],$dest_path['dirname'].'/');
    
    		$thumb->resize($settings['resize_width'], $settings['resize_height'], $settings['keep_ratio']);
    
    		if ($settings['crop_exact'] == 'true' || ($crop_x != 0 && $crop_y != 0)) {
    
    			if ($crop_x != 0 && $crop_y != 0) {
    
    				$settings['resize_width'] = $crop_x;
    
    				$settings['resize_height'] = $crop_y;
    
    			}
    
    			if ($thumb->x > $settings['resize_width' || $thumb->y > $settings['resize_height']]) {
    
    				$thumb->crop(	(int)(($thumb->x - $settings['resize_width']) / 2),
    
    								0,
    
    								$settings['resize_width'],
    
    								$settings['resize_height']
    
    							);
    
    			}
    
    		}
    
    		$thumb->outputFile($save_path."/".$rename_to, "");
    
    		if ($the_new_path) { //G2 MOD
    
    			unlink($the_new_path);
    
    		}
    
    		return tb_post_thumb_gen_image	(
    
    												$save_path.'/'.$rename_to,
    
    												$save_dir.'/'.$rename_to,
    
    												$alt_text,
    
    												$generate
    
    											);
    
    	} else {
    
    		if (!empty($settings['video_regex']) && tb_post_thumb_check_video($settings['video_regex'])) {
    
    				$settings['default_image'] = $settings['video_default'];
    
    		}
    
    		return tb_post_thumb_gen_image	(
    
    											str_replace($settings['full_domain_name'],$settings['base_path'],$settings['default_image']),
    
    											$settings['default_image'],
    
    											$alt_text,
    
    											$generate
    
    										);
    
    	}
    
    }

    Here are my plugin settings, for information :

    WORDPRESS PAGE : http://monsite.domain.com/wordpress

    DOMAIN NAME: monsite.domain.com
    DEFAULT IMAGE : http://monsite.domain.com/wordpress/wp-includes/images/post_thumbs/post_nopic.png
    FULL DOMAIN NAME : http://monsite.domain.com
    BASE PATH : /home/monsite/public_html
    FOLDER NAME : wordpress/wp-includes/images/post_thumbs

  3. ozgreg
    Member
    Posted 7 years ago #

    Hiya grosbouff,

    Thanks for your post.. However can I please get you to post this @ the WPG2 Forums

  4. grosbouff
    Member
    Posted 7 years ago #

    some fix

    function tb_post_thumb($generate=false, $alt_text='', $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0) {
    
    	global $post;
    	$settings = get_option('post_thumbnail_settings');
    	// dir to save thumbnail to
    	$save_dir = $dest_path['dirname']."/{$settings['folder_name']}"; //GALLERY2 MOD : this has been moved from above
    
    	// find an image from your domain
    
    	if (function_exists('g2_imagebypathinpost')) { //!!!because we can't use  "add_filter('get_the_content', 'g2_imagebypathinpost', 0);" into wpg2.php, see http://codex.wordpress.org/Plugin_API/Filter_Reference
    		$post_content = g2_imagebypathinpost(get_the_content());
    	} else {
    		$post_content = get_the_content;
    	}
    
    	if (preg_match('/<img (.*?)src="https?:\/\/(www\.)?'.str_replace('/','\/',$settings['domain_name']).'\/(.*?)"/i',$post_content,$matches)) {
    		$the_image = $matches[3];
    		if ( ($resize_width != 0) ||($resize_height != 0) ) {
    			$the_dimensions = '-';
    			if ($resize_width != 0) {
    				$the_dimensions .='W'.$resize_width;
    			}
    			if ($resize_height != 0) {
    				$the_dimensions .='H'.$resize_height;
    			}
    		}
    
    		// put matches into recognizable vars
    		// fix later, assumes document root will match url structure
    		$the_image = $settings['base_path'] . '/' . $matches[3];
    
    		///GALLERY2 MOD - IMGS TAGS
    		$the_image_url = $settings['full_domain_name'] . '/' . $matches[3];
    		$the_image_url = html_entity_decode($the_image_url);
    
    		$save_path = $settings['base_path'].$dest_path['dirname']."/{$settings['folder_name']}";
    		$g2_pattern = "g2_itemId=";
    		$g2_image = explode($g2_pattern, $the_image);
    
    		if(!empty($g2_image[1])) { //G2 MOD
    
    			$g2_id = explode('&', $g2_image[1]);
    			$g2_id = $g2_id[0];
    			$the_image = htmlspecialchars_decode($the_image);
    
    			$ch = curl_init(); // create cURL handle (ch)
    			if (!$ch) {
    			    die("Couldn't initialize a cURL handle");
    			}
    			// set some cURL options
    			$ret = curl_setopt($ch, CURLOPT_URL,            $the_image_url);
    			$ret = curl_setopt($ch, CURLOPT_HEADER,         1);
    			$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    			$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    			$ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);
    
    			// execute
    			$ret = curl_exec($ch);
    
    			$content_type = curl_getinfo( $ch, CURLINFO_CONTENT_TYPE );
    
    			curl_close($ch);
    
    			if (preg_match("/image\/png/", $content_type)) {
    				$new_ext = '.png';
    			}elseif (preg_match("/image\/gif/", $content_type)) {
    				$new_ext = '.gif';
    			}elseif ( (preg_match("/image\/jpg/", $content_type)) || (preg_match("/image\/jpeg/", $content_type)) ) {
    				$new_ext = '.jpg';
    			}elseif (preg_match("/image\/bmp/", $content_type)) {
    				$new_ext = '.bmp';
    			}elseif (preg_match("/image\/tiff/", $content_type)) {
    				$new_ext = '.tif';
    			}			
    
    						if ($new_ext) { //si le fichier est une image
    				$save_url = $settings['full_domain_name'].$dest_path['dirname']."/{$settings['folder_name']}";
    				$the_new_name = '/wpg2_'.$g2_id.$the_dimensions;
    
    				$the_new_path = $save_path.$the_new_name.$new_ext;
    				$the_new_url = $save_url.$the_new_name.$new_ext;
    				$the_new_thumb_url = $save_url.'.'.$the_new_name.$new_ext;
    				$the_new_thumb_path = $save_path.'.'.$the_new_name.$new_ext;
    				$the_image = $the_new_thumb_path;
    
    			}
    		}
    
    		if ((!file_exists($the_image)) && (!$the_new_thumb_path) ) {
    			return tb_post_thumb_gen_image	(
    												str_replace($settings['full_domain_name'],$settings['base_path'],$settings['default_image']),
    												$settings['default_image'],
    												$alt_text,
    												$generate
    											);
    		} elseif ($the_new_thumb_path) {
    			if (!file_exists($the_new_thumb_path)) {
    				if (copy($the_image_url, $the_new_path)){
    					chmod ($the_new_path, 0777);
    					$the_image = $the_new_path;
    				} else {
    					unset($the_new_path);
    					echo "probleme lors de la copie";
    				}
    			} else {
    				return tb_post_thumb_gen_image	(
    													$the_new_thumb_path,
    													$the_new_thumb_url,
    													$alt_text,
    													$generate
    												);
    			}
    		}
    
    		$dest_path = pathinfo($the_image);
    
    		// name to save to
    		if ($settings['append'] == 'true') {
    			$filename = substr($dest_path['basename'], 0, strrpos($dest_path['basename'], "."));
    			$rename_to = $filename.$the_dimensions.$settings['append_text'].'.'.$dest_path['extension'];
    		}
    		else $rename_to = $settings['append_text'].$dest_path['basename'];
    
    		// check if file already exists
    		// return location if does
    		if (file_exists($save_path.'/'.$rename_to)) {
    			return tb_post_thumb_gen_image	(
    												$save_path.'/'.$rename_to,
    												$save_dir.'/'.$rename_to,
    												$alt_text,
    												$generate
    											);
    		}
    
    		// sticky bit?
    		if (!is_dir($save_path)) mkdir($save_path,02775);
    		// manipulate thumbnails
    		$thumb = new ImageEditor($dest_path['basename'],$dest_path['dirname'].'/');
    
    		if ($the_dimensions) {
    			$thumb->resize($resize_width, $resize_height, $settings['keep_ratio']);
    		}else{
    			$thumb->resize($settings['resize_width'], $settings['resize_height'], $settings['keep_ratio']);
    		}
    
    		if ($settings['crop_exact'] == 'true' || ($crop_x != 0 && $crop_y != 0)) {
    			if ($crop_x != 0 && $crop_y != 0) {
    				$settings['resize_width'] = $crop_x;
    				$settings['resize_height'] = $crop_y;
    			}
    			if ($thumb->x > $settings['resize_width' || $thumb->y > $settings['resize_height']]) {
    				$thumb->crop(	(int)(($thumb->x - $settings['resize_width']) / 2),
    								0,
    								$settings['resize_width'],
    								$settings['resize_height']
    							);
    			}
    		}
    		$thumb->outputFile($save_path."/".$rename_to, "");
    
    		if ($the_new_path) { //G2 MOD
    			unlink($the_new_path);
    		}
    
    		return tb_post_thumb_gen_image	(
    												$save_path.'/'.$rename_to,
    												$save_dir.'/'.$rename_to,
    												$alt_text,
    												$generate
    											);
    	} else {
    		if (!empty($settings['video_regex']) && tb_post_thumb_check_video($settings['video_regex'])) {
    				$settings['default_image'] = $settings['video_default'];
    		}
    		return tb_post_thumb_gen_image	(
    											str_replace($settings['full_domain_name'],$settings['base_path'],$settings['default_image']),
    											$settings['default_image'],
    											$alt_text,
    											$generate
    										);
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic