Created
March 28, 2018 16:42
-
-
Save kselax/d8d8d07fc6b46ee886f002f7dc812656 to your computer and use it in GitHub Desktop.
WpThumbnail is a class for put thumbnail image to WP media library
This file contains hidden or 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 | |
| namespace ddd; | |
| if(!class_exists('WpThumbnail')){ | |
| class WpThumbnail{ | |
| private $tempImgFile; //path to temfile | |
| private $tempImgPngFile; //path to png temp file | |
| private $postId; | |
| private $imgUrl; | |
| private $width; | |
| private $height; | |
| private $headers; | |
| private $body; | |
| private $imgExt; | |
| private $imgName; | |
| private $tempImgFileL; //class LockerOfFile | |
| private $tempImgPngFileL; //class LockerOfFile | |
| private $im; | |
| private $wpImage; | |
| static public function create($postId, $imgUrl){ | |
| $obj=new self(); | |
| $obj->__construct1($postId, $imgUrl); | |
| return $obj; | |
| } | |
| public function __construct1($postId, $imgUrl){ | |
| $this->postId=$postId; | |
| $this->imgUrl=$imgUrl; | |
| $this->tempImgFile=dirname(dirname(__DIR__)).'/temp/tempImgFile'; | |
| $this->tempImgPngFile=dirname(dirname(__DIR__)).'/temp/tempImgFile.png'; | |
| } | |
| static public function addActs(){ | |
| add_action('upload_mimes', array(__CLASS__, 'my_custom_upload_mimes') ); | |
| } | |
| static public function my_custom_upload_mimes($mimes = array()) { | |
| // Add a key and value for the CSV file type | |
| $mimes['webp'] = "image/webp"; | |
| // $mimes['png']="image/png"; | |
| return $mimes; | |
| } | |
| private function setUrl(){ | |
| if($this->imgUrl[0]=='h' | |
| &&$this->imgUrl[1]=='t' | |
| &&$this->imgUrl[2]=='t' | |
| &&$this->imgUrl[3]=='p'){ | |
| return; | |
| }elseif($this->imgUrl[0]=='/'&&$this->imgUrl[1]=='/'){ | |
| $this->imgUrl='http:'.$this->imgUrl; | |
| }else{ | |
| die('WpThumbnail::setUrl imgUrl='.$this->imgUrl); | |
| } | |
| } | |
| private function setImMinSize(){ | |
| $size=self::get_image_size('large'); | |
| $this->width=$size['width']; | |
| $this->height=$size['height']; | |
| } | |
| private function readFileFromInternet(){ | |
| $response=wp_remote_get($this->imgUrl); | |
| $this->headers=wp_remote_retrieve_headers( $response ); | |
| $this->body=wp_remote_retrieve_body($response); | |
| } | |
| private function setImgExt(){ | |
| $headers=$this->headers->getAll(); | |
| switch ($headers['content-type']) { | |
| case 'image/png': | |
| $this->imgExt='png'; | |
| break; | |
| case 'image/webp': | |
| $this->imgExt='webp'; | |
| break; | |
| default: | |
| die('[die]-WpThumbnail::imgExt content-type='.$headers['content-type']); | |
| break; | |
| } | |
| }//setImgExt | |
| private function setImgName(){ | |
| $inf=pathinfo($this->imgUrl); | |
| if($inf['extension']!=''){ | |
| $this->imgName=$inf['basename']; | |
| }else if($inf['basename']!=''){ | |
| $this->imgName=$inf['basename'].'.'.$this->imgExt; | |
| } | |
| } | |
| private function createFiles(){ | |
| // first check and create | |
| if(!file_exists($this->tempImgFile)){ | |
| file_put_contents($this->tempImgFile, ''); | |
| } | |
| if(!file_exists($this->tempImgPngFile)){ | |
| file_put_contents($this->tempImgPngFile, ''); | |
| } | |
| // second check and exit | |
| if(!file_exists($this->tempImgFile)){ | |
| die("\r\nfile= ".$this->tempImgFile."\r\n"); | |
| } | |
| if(!file_exists($this->tempImgPngFile)){ | |
| die("\r\nfile= ".$this->tempImgPngFile."\r\n"); | |
| } | |
| } | |
| private function blockFiles(){ | |
| $this->tempImgFileL=new LockerOfFile($this->tempImgFile); | |
| $this->tempImgFileL->lock(); | |
| $this->tempImgPngFileL=new LockerOfFile($this->tempImgPngFile); | |
| $this->tempImgPngFileL->lock(); | |
| } | |
| private function putImgTofile(){ | |
| $this->tempImgFileL->write($this->body); | |
| } | |
| private function convertImgToPng(){ | |
| $this->im=PhpGdLib::createImgResourse($this->tempImgFile, $this->imgExt); | |
| } | |
| private function scaleImg(){ | |
| $this->im=PhpGdLib::imScale($this->im, $this->width, $this->height); | |
| } | |
| private function saveImgToFile(){ | |
| PhpGdLib::saveImToPngFile($this->im, $this->tempImgPngFile); | |
| } | |
| private function setPngImg(){ | |
| ob_start(); | |
| imagepng($this->im); | |
| $this->wpImage = ob_get_clean(); | |
| } | |
| private function setImgNameLikePng(){ | |
| if($this->imgExt=='png') | |
| return; | |
| $this->imgName=str_replace($this->imgExt, '', $this->imgName); | |
| $this->imgName=$this->imgName.'png'; | |
| } | |
| static public function imgUlrToName($imgUrl){ | |
| $name=false; | |
| $path = pathinfo($imgUrl); | |
| if($path['extension']!=''){ | |
| $name=$path['basename']; | |
| }else if($path['basename']!=''){ | |
| $name=$path['basename'].'.'.self::imgExt(self::$headers); | |
| } | |
| return $name; | |
| } | |
| private function putImageToWordpress(){ | |
| $upload = wp_upload_bits($this->imgName, null, $this->wpImage); | |
| //set post id to which you need to set post thumbnail | |
| $post_id=$this->postId; | |
| $filename = $upload['file']; | |
| // Retrieve the file type from the file name. | |
| $wp_filetype = wp_check_filetype($filename, null ); | |
| $attachment = array( | |
| 'post_mime_type' => $wp_filetype['type'], | |
| 'post_title' => sanitize_file_name($filename), | |
| 'post_content' => '', | |
| 'post_status' => 'inherit' | |
| ); | |
| // This function inserts an attachment into the media library. The function should be used in conjunction with wp_update_attachment_metadata() and wp_generate_attachment_metadata() | |
| $attach_id = wp_insert_attachment( $attachment, $filename, $post_id ); | |
| require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| // This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the | |
| $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
| // Update metadata for an attachment. | |
| wp_update_attachment_metadata( $attach_id, $attach_data ); | |
| // Sets a post thumbnail | |
| set_post_thumbnail( $post_id, $attach_id ); | |
| } | |
| public function addThumbnail($postId, $imgUrl){ | |
| // 1 - change url on absolute | |
| $this->setUrl(); | |
| $this->setImMinSize(); | |
| $this->readFileFromInternet(); | |
| $this->setImgExt(); | |
| $this->setImgName(); | |
| $this->createFiles(); | |
| $this->blockFiles(); | |
| $this->putImgTofile(); | |
| $this->convertImgToPng(); | |
| $this->scaleImg(); | |
| $this->saveImgToFile(); | |
| $this->setPngImg(); | |
| $this->setImgNameLikePng(); | |
| $this->putImageToWordpress(); | |
| }//addThumbnail | |
| /** | |
| * Get size information for all currently-registered image sizes. | |
| * | |
| * @global $_wp_additional_image_sizes | |
| * @uses get_intermediate_image_sizes() | |
| * @return array $sizes Data for all currently-registered image sizes. | |
| */ | |
| static public function get_image_sizes() { | |
| global $_wp_additional_image_sizes; | |
| $sizes = array(); | |
| foreach ( get_intermediate_image_sizes() as $_size ) { | |
| if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) { | |
| $sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" ); | |
| $sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" ); | |
| $sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" ); | |
| } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) { | |
| $sizes[ $_size ] = array( | |
| 'width' => $_wp_additional_image_sizes[ $_size ]['width'], | |
| 'height' => $_wp_additional_image_sizes[ $_size ]['height'], | |
| 'crop' => $_wp_additional_image_sizes[ $_size ]['crop'], | |
| ); | |
| } | |
| } | |
| return $sizes; | |
| }//get_image_sizes | |
| /** | |
| * Get size information for a specific image size. | |
| * | |
| * @uses get_image_sizes() | |
| * @param string $size The image size for which to retrieve data. | |
| * @return bool|array $size Size data about an image size or false if the size doesn't exist. | |
| */ | |
| static public function get_image_size( $size ) { | |
| $sizes = self::get_image_sizes(); | |
| if ( isset( $sizes[ $size ] ) ) { | |
| return $sizes[ $size ]; | |
| } | |
| return false; | |
| } | |
| }//WpThumbnail | |
| WpThumbnail::addActs(); //list of add_action | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment