Showing posts with label image. Show all posts
Showing posts with label image. 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);
}
?>

Thursday, 20 November 2014

Save image from ulr using JAVA

package com.cakes;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class SaveImageFromUrl {
 public static void main(String[] args) throws Exception {
  String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
  String destinationFile = "image.jpg";
  saveImage(imageUrl, destinationFile);
 }
 public static void saveImage(String imageUrl, String destinationFile) throws IOException {
  URL url = new URL(imageUrl);
  InputStream is = url.openStream();
  OutputStream os = new FileOutputStream(destinationFile);
  byte[] b = new byte[2048];
  int length;
  while ((length = is.read(b)) != -1) {
   os.write(b, 0, length);
  }
  is.close();
  os.close();
 }
}

Save image from url using php

<?php
$url = "http://www.technotrigger.com/wp-content/uploads/2014/01/house-in-green-field.jpg";
$content = file_get_contents($url);
file_put_contents('flower.jpg', $content);
?>
<img src="flower.jpg">

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); }

Monday, 30 June 2014

Google Maps Image APIs

Get Google Map Image for specific latittude & longitude
Static Maps API

<img src="http://maps.google.com/staticmap?size=500x400&markers=11,78&zoom=12">


<img src="http://maps.googleapis.com/maps/api/streetview?size=200x200&location=40.720032,-73.988354&heading=235">

Monday, 12 May 2014

Image uploading Instant preview




<img src="http://www.designofsignage.com/application/symbol/building/image/600x600/no-photo.jpg" width='200px' height='200px' id="preview" title="No Image"><br><br>
<input type="file"  name="postimage" id="postimage" onchange="readURL(this,200,200)" >
<script type='text/javascript' src='http://code.jquery.com/jquery-1.11.1.min.js'></script>
<script type='text/javascript'>
function readURL(input,w,h) {
 if (input.files && input.files[0]) {
  var reader = new FileReader();
  reader.onload = function (e) {
   $('#preview').attr('src', e.target.result);
   $("#preview").attr("width",w);
   $("#preview").attr("height",h);
  }
  reader.readAsDataURL(input.files[0]);
 }
}
</script>

Wednesday, 9 April 2014

A Simple PHP Thumbnail Image Resize Script




Step 1: To create Test.php & copy paste below code

<?php


session_start();
header("Pragma: public");
header("Cache-Control: max-age = 604800");
header("Expires: ".gmdate("D, d M Y H:i:s", time() + 604800)." GMT");

function thumbnail($image, $width, $height) {

 if($image[0] != "/") { // Decide where to look for the image if a full path is not given
  if(!isset($_SERVER["HTTP_REFERER"])) { // Try to find image if accessed directly from this script in a browser
   $image = $_SERVER["DOCUMENT_ROOT"].implode("/", (explode('/', $_SERVER["PHP_SELF"], -1)))."/".$image;
  } else {
   $image = implode("/", (explode('/', $_SERVER["HTTP_REFERER"], -1)))."/".$image;
  }
 } else {
  $image = $_SERVER["DOCUMENT_ROOT"].$image;
 }
 $image_properties = getimagesize($image);
 $image_width = $image_properties[0];
 $image_height = $image_properties[1];
 $image_ratio = $image_width / $image_height;
 $type = $image_properties["mime"];

 if(!$width && !$height) {
  $width = $image_width;
  $height = $image_height;
 }
 if(!$width) {
  $width = round($height * $image_ratio);
 }
 if(!$height) {
  $height = round($width / $image_ratio);
 }

 if($type == "image/jpeg") {
  header('Content-type: image/jpeg');
  $thumb = imagecreatefromjpeg($image);
 } elseif($type == "image/png") {
  header('Content-type: image/png');
  $thumb = imagecreatefrompng($image);
 } else {
  return false;
 }

 $temp_image = imagecreatetruecolor($width, $height);
 imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
 $thumbnail = imagecreatetruecolor($width, $height);
 imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height);

 if($type == "image/jpeg") {
  imagejpeg($thumbnail);
 } else {
  imagepng($thumbnail);
 }

 imagedestroy($temp_image);
 imagedestroy($thumbnail);

}

if(isset($_GET["h"])) { $h = $_GET["h"]; } else { $h = 0; }
if(isset($_GET["w"])) { $w = $_GET["w"]; } else { $w = 0; }

thumbnail($_GET["img"], $w, $h);

?>


Step 2: Index.php



Wednesday, 24 April 2013

Funny Pinterest widget


Pinterest


Pinterest Widget