Last active
August 29, 2015 14:03
-
-
Save snoise/1f5d7511505fe09a5db0 to your computer and use it in GitHub Desktop.
画像をアップロード時に、画像の処理をするサンプル#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: PakaUploadImg | |
Plugin URI: http://www.paka3.com/wpplugin | |
Description: 画像をアップロード時に、画像の処理をする | |
Author: Shoji ENDO | |
Version: 0.1 | |
Author URI:http://www.paka3.com/ | |
*/ | |
$a = new Paka3UploadImg ; | |
class Paka3UploadImg{ | |
function __construct(){ | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
add_filter( "wp_generate_attachment_metadata" , array( $this , "paka3_img_meta" ),10 ,2 ); | |
} | |
function paka3_img_meta($metadata, $imgID){ | |
$imgPath = get_attached_file( $imgID ); | |
$mime_type = get_post_mime_type( $imgID ); | |
//ここで、条件を[jpgのみとかファイル名にhoge_がついているとか] | |
if( $mime_type == 'image/jpeg' ){ | |
//*ここで画像処理をする | |
//メインファイル変換 | |
$im = $this->paka3_imagecreate( $imgPath ); | |
//フィルター | |
$im = $this->imageGdfunc($im); | |
//メインファイル保存 | |
$res = $this->paka3_image($im,$imgPath); | |
imagedestroy($im); | |
//print_r( $metadata['sizes']); | |
foreach ( $metadata['sizes'] as $akey => $aval) { | |
//サブファイル名を取得 | |
$sub_imgPath = dirname($imgPath).'/'.$aval[ 'file' ]; | |
//ここで画像処理をする | |
//サブメインファイル変換 | |
$sub_im = $this->paka3_imagecreate( $sub_imgPath ); | |
//フィルター | |
$sub_im = $this->imageGdfunc( $sub_im ); | |
//メインファイル保存 | |
$res = $this->paka3_image($sub_im,$sub_imgPath ); | |
imagedestroy( $sub_im ); | |
} | |
} | |
return $metadata ; | |
} | |
//###################### | |
//GDのフィルターを使ってみる | |
//###################### | |
private function imageGdfunc($im){ | |
if($im && imagefilter($im, IMG_FILTER_GRAYSCALE)){ | |
//$str = '※変換が成功しました。'; | |
return $im; | |
}else{ | |
//$str = '変換が失敗しました。'; | |
return false; | |
} | |
} | |
//###################### | |
//画像のイメージ作成(jpeg/png/gif) | |
//###################### | |
private 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; | |
} | |
//###################### | |
//画像の保存 | |
//###################### | |
private 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