CodeIgniter上傳圖檔以及縮圖

[php]
// 縮圖
function _createThumbnail($fileName, $isThumb, $thumbMarker="", $width, $height) {
// 參數
$config[‘image_library’] = ‘gd2’;
$config[‘source_image’] = $fileName;
$config[‘create_thumb’] = $isThumb;
$config[‘maintain_ratio’] = TRUE;
$config[‘master_dim’] = ‘width’;
if(isset($thumbMarker) && $thumbMarker!=""){
$config[‘thumb_marker’] = $thumbMarker;
}
$config[‘width’] = $width;
$config[‘height’] = $height;

$this->load->library(‘image_lib’, $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
[/php]
上傳
[php]
function uploadimage(){
$name = $this->input->get_post(‘my_file’);
$config[‘upload_path’] = ‘./uploads/’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘file_name’] = $name;
$config[‘overwrite’] = true;
$config[‘max_size’] = 4096;
//$config[‘max_width’] = 5000;
//$config[‘max_height’] = 5000;
$this->load->library(‘upload’, $config);
if ( ! $this->upload->do_upload())
{
$data[‘errormsg’] = array(‘error’ => $this->upload->display_errors());
$this->load->view(‘welcome_message’, $data);
}
else
{
$fInfo = $this->upload->data();
$this->_createThumbnail($fInfo[‘full_path’],TRUE,"",330,480);
$this->_createThumbnail($fInfo[‘full_path’],TRUE,"_tn",110,160);

$data[‘filename’]=$this->upload->data(‘file_name’);
$this->load->view(‘welcome_message’,$data);
}

}
[/php]

取得vine影片的og:image方式


Youtube影片在分享時,是很有規則的縮圖
http://img.youtube.com/vi/VIDEO_ID/#.jpg
0.jpg (預設)480×360
1.jpg 第一張縮圖
2.jpg 第二張縮圖
3.jpg 第三張縮圖
以及依品質的縮圖
default.jpg
hqdefault.jpg
mqdefault.jpg
sddefault.jpg
maxresdefault.jpg
不過vine沒有這樣的規則,所以只好自己抓取影片頁面裡的og:image了!
建立一個 get_vine_thumbnail.php

function get_vine_thumbnail($id){
	$vine = file_get_contents("https://vine.co/v/{$id}");
	preg_match('/property="og:image" content="(.*?)"/', $vine, $matches); 
	return ($matches[1]) ? $matches[1] : false;
}
$id1 = $_GET['id'];
header('Content-type: image/jpeg');  
$image = imagecreatefromjpeg(get_vine_thumbnail($id1));  
imagejpeg($image); 

使用時就是 get_vine_thumbnail.php?id=xxxx就可以了!

相關連結:
https://developers.google.com/youtube/v3/

http://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api

https://developers.google.com/youtube/2.0/developers_guide_php?csw=1

PHP產生透明的png縮圖

二個重點函數
imagealphablending($thumb,false)
imagesavealpha($thumb,true);

[php]
function pngthumb($sourePic,$smallFileName,$width,$heigh){
$image=imagecreatefrompng($sourePic);//PNG
imagesavealpha($image,true);
$BigWidth=imagesx($image);
$BigHeigh=imagesy($image);
$thumb = imagecreatetruecolor($width,$heigh);
imagealphablending($thumb,false);
imagesavealpha($thumb,true);
if(imagecopyresampled($thumb,$image,0,0,0,0,$width,$heigh,$BigWidth,$BigHeigh)){
imagepng($thumb,$smallFileName);}
return $smallFileName;
}
[/php]