fix to make it work with WPG2 plugin & other php-generated images calls.
-
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
https://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 ('&','&', $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 ); } }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘fix to make it work with WPG2 plugin & other php-generated images calls.’ is closed to new replies.