Skip to content

Instantly share code, notes, and snippets.

@vihoangson
Created August 21, 2015 09:09
Show Gist options
  • Save vihoangson/f430053f9668580f925b to your computer and use it in GitHub Desktop.
Save vihoangson/f430053f9668580f925b to your computer and use it in GitHub Desktop.
<?php
/**
* class Get_img_in_content
* @todo: Automatic save image in content
* @author: Vi Hoang Son
* @version: 1 - 2015
*/
class Get_img_in_content
{
var $dir;// direct default
var $max_width;//Giá trị tối đa của hình ảnh được xử lý, nếu lớn hơn sẽ dùng hình ảnh của trang nguồn
var $max_size;
function __construct(){
if(defined(PATH_UPLOAD_IMG)){
throw new Exception("Don't define PATH_UPLOAD_IMG", 1);
}
// direct default
// Default value: /uploads/img
$this->dir = defined(AUTOSAVEIMG_MAXWIDTH)? FCPATH.PATH_UPLOAD_IMG : FCPATH."uploads/img";;
//Giá trị tối đa của hình ảnh được xử lý, nếu lớn hơn sẽ dùng hình ảnh của trang nguồn
//Default value: 5000
$this->max_width = defined(AUTOSAVEIMG_MAXWIDTH)? AUTOSAVEIMG_MAXWIDTH : 5000;
//Giá trị tối đa của hình ảnh được xử lý, nếu lớn hơn sẽ dùng hình ảnh của trang nguồn
//Default value: 10000
$this->max_size = defined(AUTOSAVEIMG_MAXSIZEFILE)? AUTOSAVEIMG_MAXSIZEFILE : 10000;;
//if don't have direc then make new dir
if(!is_dir($this->dir)){
@mkdir($this->dir);
}
}
//=========================================================
/*
* Automatic save image in content
* @param: string
* @return: string
*/
public function do_get_img_from_content($data=array()){
if(!AUTOSAVEIMG_FLAG) {
return $data["content"];
}
list($title,$content) = $data;
$remote_images=$this->get_all_img($content);
if(!$remote_images) return $content;
$file="";
$i=1;
foreach ($remote_images as $key => $link) {
if(!preg_match("/^http:\/\/|https:\/\//", $link)){
continue;
}
list($width, $height, $type, $attr) = @getimagesize($link);
if($width > $this->max_width || $width > $this->max_width ) continue;
$file=$this->download_image($link);
$type= $this->getFileType($file);
$filename=$this->get_filename_from_url($link);
if(!$filename){
$filename=md5(time()).".".$type;
}
$filename=mod_rewrite($title)."_".$i.".".$type;
if(!file_put_contents($this->dir."/".$filename, $file)) {
continue;
}else{
$i++;
}
$content = str_replace($value, base_url().$this->dir."/".$filename, $content);
}
return $content;
}
//=========================================================
/**
*
* @param
*
* @return
* @author vihoangson
**/
public function get_all_img($content){
$remote_images = array();
$preg = preg_match_all('/<img.*?src=\"((?!\").*?)\"/i', stripslashes($content), $matches);
if ($preg) $remote_images = $matches[1];
$preg = preg_match_all('/<img.*?src=\'((?!\').*?)\'/i', stripslashes($content), $matches);
if ($preg) $remote_images = array_merge($remote_images, $matches[1]);
if(is_array($remote_images))
return $remote_images;
return false;
}
//=========================================================
/**
*
* @param
*
* @return
* @author vihoangson
**/
public function getFileType($file){
$bin = substr($file,0,2);
$strInfo = @unpack("C2chars", $bin);
$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
switch ($typeCode) {
case 7790: $fileType = 'exe'; return false;
case 7784: $fileType = 'midi'; return false;
case 8297: $fileType = 'rar'; return false;
case 255216: $fileType = 'jpg'; $mime = 'image/jpeg'; return $fileType;
case 7173: $fileType = 'gif'; $mime = 'image/gif'; return $fileType;
case 6677: $fileType = 'bmp'; $mime = 'image/bmp'; return $fileType;
case 13780: $fileType = 'png'; $mime = 'image/png'; return $fileType;
default: return false;
}
}
//=========================================================
/**
*
* @param
*
* @return
* @author vihoangson
**/
public function download_image($image_url) {
$file = '';
// file_get_contents
if (function_exists('file_get_contents')) {
$file = @file_get_contents($image_url);
}
// curl
if (!$file && function_exists('curl_init')) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
}
$img = @imagecreatefromstring($file);
// GD
if (!$img && function_exists('fsockopen')) {
try{
$type = $this->fsockopen_image_header($image_url);
if ($type && in_array($type, array('image/jpeg', 'image/gif', 'image/png'))) {
$type = substr($type, 6);
$img = call_user_func("imagecreatefrom{$type}", $image_url);
ob_start();
call_user_func("image{$type}", $img);
$file = ob_get_contents();
ob_end_clean();
imagedestroy($img);
} else {
$file = '';
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
if(!$file){
return false;
}
return $file;
}
//=========================================================
/**
* get_filename_from_url
* @param
* $url(text)
* @return name of url
* @author vihoangson
**/
public function get_filename_from_url($url) {
$url = parse_url($url);
$path = $url['path'];
$filename = explode('/', $path);
return $filename[count($filename)-1];
}
//=========================================================
/**
* get_img()
* Save hình về local khi truyền vào link.
*
* @param
* $link(string)
* $title(string)
* $options=array(
* "return"=>"info_file",
* )
* @return void
* @author vihoangson
**/
public function get_img($link,$title=null,$options=array()){
if(strlen($title)>20){throw new Exception("String too long", 100);}
if(!$title){
$title="img-".time();
}
$i=time();
list($width, $height, $type, $attr) = @getimagesize($link);
if($width > $this->max_width || $width > $this->max_width){
return false;
}
if(!preg_match("/^http:\/\/|https:\/\//", $link)){
$file=$this->download_image($link);
}
$type= $this->getFileType($file);
$filename=$this->get_filename_from_url($link);
if(!$filename){
$filename=md5(time()).".".$type;
}
$filename=mod_rewrite($title)."_".$i.".".$type;
if(!file_put_contents($this->dir."/".$filename, $file)) {
throw new Exception("Can't write file", 500);
}else{
if(!isset($options["return"])) return true;
switch($options["return"]){
case "info_file":
return array(
"name_file" => $filename,
"link_file" => "/".PATH_UPLOAD_IMG."/".$filename,
);
break;
default:
return true;
break;
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment