Showing posts with label GD functions. Show all posts
Showing posts with label GD functions. Show all posts

Thursday, 31 May 2018

Image Resize and save by using GD function and Percentage based

<?php
$filename = 'test.jpg';
$percent = .5;
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$ourFileName = "newtest.jpg";
imagejpeg($thumb, $ourFileName);
imagedestroy($thumb);
?>

Friday, 18 May 2018

PNG to Jpeg in PHP using GD library


<?php
function png2jpg($originalFile, $outputFile, $quality) {
    $image = imagecreatefrompng($originalFile);
    imagejpeg($image, $outputFile, $quality);
    imagedestroy($image);
}
png2jpg("img/test.png", "img/".time().".jpg", 100);
if ($handle = opendir('img/')) {
	$i=0;
   while (false !== ($entry = readdir($handle))) {
      if ($entry != "." && $entry != "..") {
         echo "<img src='img/$entry' width=150 height=100>    ";
			if($i%10==0 && $i!=0){
				echo "<br>";
			}
			$i++;
      }
   }
   closedir($handle);
}
?>

Saturday, 16 August 2014

To add watermark image with original image by using php GD functions

$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 80; // distance to border in pixels for watermark image
$opacity = 20; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding+10;
$dest_y = $image_size[1] - $watermark_height - $padding-80;
// copy watermark on main image
imagecopymerge
($image, $watermark,$dest_x, $dest_y, 0, 0, 
$watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark); 

To Crop image using PHP GD function

cropImage(150,100, 'example.png', 'png', 'image.png');
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
echo "
".$w = 500; echo "
".$h = 400; switch($stype) { case 'gif': $simg = imagecreatefromgif($source); break; case 'jpg': $simg = imagecreatefromjpeg($source); break; case 'png': $simg = imagecreatefrompng($source); break; } $dimg = imagecreatetruecolor($nw, $nh); echo "
".$wm = $w/$nw; echo "
".$hm = $h/$nh; echo "
".$h_height = $nh/2; echo "
".$w_height = $nw/2; if($w> $h) { echo "
".$adjusted_width = $w / $hm; echo "
".$half_width = ($adjusted_width / 2)+25; echo "
".$int_width = $half_width - $w_height; imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); } elseif(($w <$h) || ($w == $h)) { $adjusted_height = $h / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); } else { imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); } imagejpeg($dimg,$dest,100); }