Sunday, July 18, 2010

create dynamic images using php

$url = "http://technmarketing.com/wp-content/uploads/2009/03/twitter-wallpaper2.jpg";

//Create Image By Url Method - 1
function create_image_by_url1($url)
{
$target_path = $url;

$imgdir="";

$path_parts = pathinfo($url);

$file_name = $path_parts['basename'];

$imgext = $path_parts['extension'];

if($imgext=='jpg' || $imgext=='JPG')
$src_img=imagecreatefromjpeg($target_path);
if($imgext=='gif' || $imgext=='GIF')
$src_img=imagecreatefromgif($target_path);
if($imgext=='png' || $imgext=='PNG')
$src_img=imagecreatefrompng($target_path);
if($imgext=='jpeg' || $imgext=='jpeg')
$src_img=imagecreatefromjpeg($target_path);

$size = getimagesize($target_path);

$dst_img = imagecreatetruecolor($size[0],$size[1]);

imagecopyresampled($dst_img,$src_img,0,0,0,0,$size[0],$size[1],$size[0],$size[1]);

if(($imgext=='jpg') || ($imgext=='JPG') || ($imgext=='jpeg') || ($imgext=='JPEG'))
{
imagejpeg($dst_img, $imgdir.$file_name,90);
}else if(($imgext=='gif') || ($imgext=='GIF')){
imagegif($dst_img, $imgdir.$file_name,90);
}else if(($imgext=='png') || ($imgext=='PNG')){
imagepng($dst_img, $imgdir.$file_name,5);
}
}
create_image_by_url1($url);

No comments:

Post a Comment