Last active
August 29, 2015 13:57
-
-
Save snoise/9897235 to your computer and use it in GitHub Desktop.
2つの画像を合成、サムネイルを再構成#WordPressプラグイン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Paka3Reimg | |
Plugin URI: http://www.paka3.com/wpplugin | |
Description: 2つの画像を合成、サムネイルを再構成 | |
Author: Shoji ENDO | |
Version: 0.1 | |
Author URI:http://www.paka3.com/ | |
*/ | |
//オブジェクトを生成 | |
new Paka3Reimg; | |
//クラス定義 | |
class Paka3Reimg { | |
//コンストラクタ | |
function __construct() { | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
add_action('admin_menu', array($this, 'adminAddMenu')); | |
} | |
//###################### | |
//管理メニューの設定 | |
//###################### | |
function adminAddMenu() { | |
add_submenu_page("options-general.php", 'Paka3画像加工', 'Paka3画像加工', 'edit_themes', 'paka3_re_img', array($this,'paka3_re_img')); | |
} | |
//###################### | |
//画像の画面と処理 | |
//###################### | |
function paka3_re_img() { | |
//合成元の画像のIDとパス | |
$_POST['imgID'] = esc_html($_POST['imgID']); | |
$imgID=$_POST['imgID']; | |
$imgPath = get_attached_file( $_POST['imgID']); | |
//合成する画像のIDとパス | |
$_POST['imgID2'] = esc_html($_POST['imgID2']); | |
$imgID2=$_POST['imgID2']; | |
$imgPath2 = get_attached_file( $_POST['imgID2']); | |
if($imgPath && $imgPath2 &&isset($_POST['reImg']) && check_admin_referer('paka3reimage')){ | |
$imgURL = wp_get_attachment_link( $imgID ); | |
if($_POST['save']==1){ | |
//上書き | |
$str="[上書きされました(ID:".$imgID.")]<br />"; | |
}else{ | |
//新しい画像を作成 | |
$str="[複製されました(ID:".$imgID.")]"; | |
$d=date("U"); | |
$newImgPath = dirname($imgPath).'/'.$d."_". basename( $imgPath ); | |
if(copy($imgPath,$newImgPath)){ | |
//同一ディレクトリに保存 | |
$wp_filetype = wp_check_filetype(basename($newImgPath), null ); | |
$attachment = array( | |
'guid' => $imgURL . '/' . basename( $newImgPath ), | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => preg_replace('/\.[^.]+$/', '', basename($newImgPath)), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$imgID = wp_insert_attachment( $attachment, $newImgPath , 0 ); | |
$imgPath = get_attached_file( $imgID ); | |
$str.="=>[ID:".$imgID."]<br />"; | |
} | |
} | |
//画像を合成する関数 | |
$str .= $this->imageMergePic($imgPath,$imgPath2); | |
//サムネイル画像の再作成 | |
$metadata = wp_generate_attachment_metadata( $imgID , $imgPath ); | |
if (!empty( $metadata ) && ! is_wp_error( $metadata ) ) { | |
wp_update_attachment_metadata( $imgID , $metadata ); | |
}else{ | |
$str .= "ID".$imgID."は、再構成されませんでした(エラー)。<br />"; | |
exit(); | |
} | |
//更新メッセージ | |
$img = wp_get_attachment_image($imgID); | |
$url = get_admin_url('','post.php')."?post={$imgID}&action=edit"; | |
echo '<div class="updated fade"><p><strong>'; | |
echo '画像の再構成が実行されました<br />'.$str; | |
echo "<a href={$url} target=_blank>{$img}</a></strong></p></div>"; | |
} | |
$wp_n = wp_nonce_field('paka3reimage'); | |
echo <<< EOS | |
<div class="wrap"> | |
<h2>画像と画像を合成してみる。</h2> | |
右下に画像を合成します(アイキャッチ画像など、画像を再構成します。)<br /><br /> | |
<div class="paka3class"> | |
<form action="" method="post"> | |
{$wp_n} | |
<input name="reImg" type="hidden" value="1"/> | |
合成元の画像ID:<input name="imgID" type="text" value=""/><br /> | |
合成する画像ID:<input name="imgID2" type="text" value=""/><br /> | |
<label for="save"> | |
<input id="save" name="save" type="checkbox" value="1"/> | |
上書きする</label> | |
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="フィルターを適用する" /></p> | |
</form> | |
</div> | |
</div> | |
EOS; | |
} | |
//###################### | |
//画像と画像をを合成する例 | |
//###################### | |
function imageMergePic($imgPath,$imgPath2){ | |
//合成元 | |
$im01 = $this->paka3_imagecreate($imgPath); | |
$im01X = 0; | |
$im01Y = 0; | |
//合成する画像 | |
$im02 = $this->paka3_imagecreate($imgPath2); | |
$im02Width = imagesx($im02); | |
$im02Height = imagesy($im02); | |
//右下 | |
$im01X=imagesx($im01)-$im02Width; | |
$im01Y=imagesy($im01)-$im02Height; | |
//合成 | |
//透過を適用する場合はこっち | |
if($im01 && $im02 && imagecopy($im01, $im02, $im01X, $im01Y, 0, 0, $im02Width, $im02Height)){ | |
$str = '※変換が成功しました。<br />'; | |
//保存 | |
$this->paka3_image($im01, $imgPath); | |
imagedestroy($im01);imagedestroy($im02); | |
}else{ | |
$str = '変換が失敗しました。<br />'; | |
} | |
return $str; | |
} | |
//###################### | |
//画像のイメージ作成(jpeg/png/gif) | |
//###################### | |
function paka3_imagecreate($imgPath){ | |
$mime = wp_check_filetype(basename($imgPath), null ); | |
if($mime['type'] == "image/jpeg"){ | |
$im = imagecreatefromjpeg($imgPath); | |
}elseif($mime['type'] == "image/png"){ | |
$im = imagecreatefrompng($imgPath); | |
}elseif($mime['type'] == "image/gif"){ | |
$im = imagecreatefromgif($imgPath); | |
} else{ | |
$im = false; | |
} | |
return $im; | |
} | |
//###################### | |
//画像の保存 | |
//###################### | |
function paka3_image($im,$imgPath){ | |
$mime = wp_check_filetype(basename($imgPath), null ); | |
if($mime['type'] == "image/jpeg"){ | |
imagejpeg($im, $imgPath); | |
}elseif($mime['type'] == "image/png"){ | |
imagepng($im, $imgPath); | |
}elseif($mime['type'] == "image/gif"){ | |
imagegif($im, $imgPath); | |
}else{ | |
return false; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment