PHP Curl Save remote server picture

Source: Internet
Author: User
Tags curl imagejpeg strlen

PHP Curl Save remote server picture

function Loadimagecurl ($save _to) {
$ch = Curl_init ($this->source);
$fp = fopen ($save _to, "WB");

Set URL and other appropriate options
$options = Array (curlopt_file => $fp,
Curlopt_header => 0,
Curlopt_followlocation => 1,
Curlopt_timeout => 60); 1 minute timeout (should be enough)

Curl_setopt_array ($ch, $options);

Curl_exec ($ch);
Curl_close ($ch);
Fclose ($FP);

}

$info = @GetImageSize ($this->source);
$mime = $info [' MIME '];

What sort of image?
$type = substr (STRRCHR ($mime, '/'), 1);
Switch ($type) {
Case ' JPEG ':
$image _create_func = ' imagecreatefromjpeg ';
$image _save_func = ' imagejpeg ';
$new _image_ext = ' jpg ';

Best quality:100
$quality = IsSet ($this->quality)? $this->quality:100;
Break

Case ' PNG ':
$image _create_func = ' imagecreatefrompng ';
$image _save_func = ' imagepng ';
$new _image_ext = ' png ';

Compression Level:from 0 (no Compression) to 9
$quality = IsSet ($this->quality)? $this->quality:0;
Break

Case ' BMP ':
$image _create_func = ' imagecreatefrombmp ';
$image _save_func = ' imagebmp ';
$new _image_ext = ' bmp ';
Break

Case ' gif ':
$image _create_func = ' imagecreatefromgif ';
$image _save_func = ' imagegif ';
$new _image_ext = ' gif ';

Break

Case ' vnd.wap.wbmp ':
$image _create_func = ' imagecreatefromwbmp ';
$image _save_func = ' imagewbmp ';
$new _image_ext = ' bmp ';
Break

Case ' XBM ':
$image _create_func = ' IMAGECREATEFROMXBM ';
$image _save_func = ' IMAGEXBM ';
$new _image_ext = ' XBM ';
Break

Default
$image _create_func = ' imagecreatefromjpeg ';
$image _save_func = ' imagejpeg ';
$new _image_ext = ' jpg ';
}

if (IsSet ($this->set_extension)) {
$ext = STRRCHR ($this->source, ".");
$strlen = strlen ($ext);
$new _name = basename (substr ($this->source, 0,-$strlen)). $new _image_ext;
}else{
$new _name = basename ($this->source);
}

$save _to = $this->save_to. " /blog_insert_temp_ ". Time (). Mt_rand (1,99).". $new _image_ext;
Output object composition is the same as the $_file variable after the same as the normal image upload processing
$img _info[' name '] = basename ($this->source);
$img _info[' type '] = $mime;
$img _info[' size '] = 1000;
$img _info[' tmp_name '] = $save _to;
$img _info[' ERROR '] = 0;

if ($method = = ' Curl ') {
$save _image = $this->loadimagecurl ($save _to);
}elseif ($method = = ' gd ') {
$img = $image _create_func ($this->source);

if (IsSet ($quality)) {
$save _image = $image _save_func ($img, $save _to, $quality);
}else{
$save _image = $image _save_func ($img, $save _to);
}

}
return $img _info;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.