| Server IP : 103.48.194.25 / Your IP : 216.73.217.33 Web Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 System : Linux VMHostDefault 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 User : sieuthimay ( 1016) PHP Version : 8.2.20 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/zomuaban.com/public_html/webadmin/libraries/ |
Upload File : |
<?php
/**
* @author: Vo Van Hieu
* @email : vovanhieu2020@gmail.com
* @method : download image from url
* @date : 23/08/2012
*/
class download_image
{
private $upload_image_to = "uploads/images";
private $image_restrict = "jpg,bmp,jpeg,png,gif";
private $image_size_restrict = 99999999999;
private $type_get = 1; //1:get by curl ,2:fopen ,default: get by file_get_contents
private $err = '';
public function __construct($upload_image_to='',$image_restrict='',$image_size_restrict='')
{
if($upload_image_to!='')
$this->upload_image_to = $upload_image_to;
if($image_restrict!='')
$this->image_restrict = $image_restrict;
if($image_size_restrict!='')
$this->image_size_restrict = $image_size_restrict;
}
public function get_img($image_link)
{
return $this->uploadUrl($image_link,$this->upload_image_to."/",$this->image_restrict,$this->image_size_restrict);
}
public function multi_get_img(&$arr_obj_img)
{
$mh = curl_multi_init();
$curl_array = array();
foreach($arr_obj_img as $i => $obj)
{
$curl_array[$i] = curl_init($obj->src);
curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_array[$i], CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl_array[$i], CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_array[$i], CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl_array[$i], CURLOPT_TIMEOUT,30);
curl_multi_add_handle($mh, $curl_array[$i]);
}
$running = NULL;
do {
usleep(10000);
curl_multi_exec($mh,$running);
} while($running > 0);
foreach($arr_obj_img as $i => &$obj)
{
$content = curl_multi_getcontent($curl_array[$i]);
$obj->arr_img_info = $this->multi_donwload_img($content,$obj);
}
foreach($arr_obj_img as $i => $url){
curl_multi_remove_handle($mh, $curl_array[$i]);
}
curl_multi_close($mh);
}
public function multi_donwload_img($content,$obj_img)
{
$savePath = $this->upload_image_to."/";
$result =array();
$url=$obj_img->src;
if($url!="")
{
$url = str_replace(" ","%20",$url);
//$filename_encode = $this->get_url_filename($url);
//$url1 = str_replace ($filename_encode,urlencode($filename_encode),$url);
//echo $url;
$size = getimagesize($url);
//echo $obj_img->src;
//echo $size;exit();
$ext=$this->check_type_img($size, $content);
if($ext)
{
$type_upload = explode(',',$this->image_restrict);
if(!in_array($ext,$type_upload)){
$this->setErr("Can't get this image because it is incorrect format of image.<br/>");
}else{
if(!file_exists($savePath)){
$this->makeDir( $savePath ) ;
}
$name = time().mt_rand(0,50000).'.'.$ext;//basename($url);
$fn = $savePath.$name;
$fp = fopen($fn,"w");
fwrite($fp,$content,strlen($content));
fclose($fp);
$result['name'] = $name;
$result['ext'] = $ext;
$result['url'] =$url;
$result['save_path']=$savePath;
$result['file_size']=filesize($fn);
}
}
}
return $result;
}
function get_url_filename($url)
{
$arr = explode("/",$url);
$filename = array_pop($arr);
$ext = substr($filename,strpos($filename,"."));
if (substr($ext,4))
{
if (substr($ext,4,1)=="?"){
$ext = substr($ext,0,4);
}else{
$ext = substr($ext,0,5);
}
}
$filename = substr($filename,0,strpos($filename,"."));
return $filename . $ext;
}
function uploadUrl($url,$savePath,$imageRestrict,$imageSizeRestrcit)
{
$result =array();
if($url!="")
{
$content="";
$url = str_replace(" ","%20",$url);
switch ($this->type_get)
{
case 1:
// init curl
$ch = curl_init($url);
if (! $ch)
{
die( "Lỗi trong quá trình khởi tạo cURL" );
}
// Chúng ta sẽ nhận trị trả về và nhận dưới dạng binary
// Dó đó ta phải set 2 option như sau
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,30);
// Nếu không set như trên, bạn sẽ không nhận được gì
// khi curl được thực thi, tức giá trị NULL
// run curl and save value into data
$content = curl_exec($ch);
// close connect
curl_close($ch);
break;
case 2:
$file=fopen($url,'rb');
while (!feof($file))
{
$content.= fread($file, 8192);
}
fclose($file);
break;
default:
$content = file_get_contents($url);
break;
}
/*BEgin End code file name to get imagesize for case url invalid*/
//$filename_encode = $this->get_url_filename($url);
//$url1 = str_replace ($filename_encode,urlencode($filename_encode),$url);
/*End End code*/
$size = getimagesize($url);
$ext=$this->check_type_img($size, $content);
//echo $ext.'|'.$url.'<br/>';
if($ext){
$type_upload = explode(',',$imageRestrict);
if(!in_array($ext,$type_upload)){
$this->setErr("Can't get this image because it is incorrect format of image.<br/>");
}else{
if(!file_exists($savePath)){
$this->makeDir( $savePath ) ;
}
$name = time().mt_rand(0,50000).'.'.$ext;//basename($url);
$fn = $savePath.$name;
$fp = fopen($fn,"w");
fwrite($fp,$content,strlen($content));
fclose($fp);
$result['name'] = $name;
$result['ext'] = $ext;
$result['url'] =$url;
$result['save_path']=$savePath;
$result['file_size']=filesize($fn);
}
}
}
return $result;
}
function check_type_img($size,$fp)
{
if ($size && $fp) {
switch ($size['mime']) {
case "image/gif":
return "gif";
break;
case "image/jpeg":
return "jpg";
break;
case "image/png":
return "png";
break;
case "image/bmp":
return "bmp";
break;
case "application/x-shockwave-flash":
return "swf";
break;
case "image/psd";
return "psd";
break;
}
} else {
return false;
}
}
function makeDir( $target )
{
// from php.net/mkdir user contributed notes
$target = str_replace( '//', '/', $target );
if ( file_exists( $target ) )
return @is_dir( $target );
// Attempting to create the directory may clutter up our display.
if ( @mkdir( $target ) ) {
$stat = @stat( dirname( $target ) );
$dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
@chmod( $target, $dir_perms );
return true;
} elseif ( is_dir( dirname( $target ) ) ) {
return false;
}
// If the above failed, attempt to create the parent node, then try again.
if ( ( $target != '/' ) && ( $this->makeDir( dirname( $target ) ) ) )
return $this->makeDir( $target );
return false;
}
public function setErr($err)
{
$this->err .=$err;
}
public function getErr()
{
return $this->err;
}
}