Showing posts with label Resize Image. Show all posts
Showing posts with label Resize Image. Show all posts

Thursday, April 7, 2016

Determine Image Dimension based on Font size in PHP


Working with images and text, we need to set image size based on font size is very useful for dynamic image processing. like CAPTCHA and text images. if you looking for PHP script to set image size based on font size here is the solution for you.

<?php


if($_GET['fontsize']!="" and is_numeric($_GET['fontsize']))
$size=$_GET['fontsize'];
else
$size=20;


define("F_SIZE", $size);
define("F_ANGLE", 0);
define("F_FONT", "verdana.ttf");


$text = "W3 Lessons";

$Leading=0;
$W=0;
$H=0;
$X=0;
$Y=0;

$_bx = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$s = preg_split("/\n]+/", $text);  // Array of lines
$nL = count($s);  // Number of lines

$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+2+($nL>1?($nL*$Leading):0):$H;

$W*=1.05;
$H*=1.5;

$img = @imagecreate($W+8, $H) or die("Cannot Initialize new GD image stream");

$white = imagecolorallocate($img, 255,255,255);
$txtColor = imagecolorallocate($img, 29,56,131);

// Adjust padding right:
$W+=8;
$bgwhite = imagecolorallocatealpha($img,238,238,238,0);
imagefilledrectangle($img, 0, 0,$W,$H, $bgwhite);

$alpha = "".range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);

// Use the string to determine the height of a line
$_b = imageTTFBbox(F_SIZE, F_ANGLE, F_FONT, $alpha);
$_H = abs($_b[5]-$_b[3]);
$__H = 0;

// Use the string to determine the width of a line
$_b = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$_W = abs($_b[2]-$_b[0]);

// Final width and height
$_X = abs($W/2)-abs($_W/2);
$__H += $_H;

imagettftext($img, F_SIZE, F_ANGLE, $_X, $__H, $txtColor, F_FONT, $text);

header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
// END EMAIL IMAGE  
exit();
?>

Thursday, September 27, 2012

Resize Image when upload PHP


Upload and Resize Image dynamically. when upload a image file in PHP is very use full to manage images on web development.
here is my solution how to upload image after resize  uploaded image in PHP


Upload Rezied Image on Server

Upload and Resize Image PHP

Upload-Resize.php (full Source)

<?php

if(isset($_POST['upload_image']))
{
 
 // Check Valid Image 
 if(preg_match("/(jpg)|(png)|(gif)/i",$_FILES['image_file']['name']))
 {
   
 // Load uploaded image from $_FILES
 // encode type in form need to set | encode="multipart/form-data" |
 $filename = $_FILES['image_file']['tmp_name'];
 
 // Prepare target image name with path on server appending with timestamp
 $target_file_name = "Image".time();
 $target_file_name = "images/$target_file_name";  
 
 $image_info = getimagesize($filename);
 $image_type = $image_info[2];


  
  if( $image_type == IMAGETYPE_JPEG ) {
   $image = imagecreatefromjpeg($filename);
   $target_file_name.=".jpg"; // append file extention based on image type;
  } elseif( $image_type == IMAGETYPE_GIF ) {
   $image = imagecreatefromgif($filename);
   $target_file_name.=".gif";  
  } elseif( $image_type == IMAGETYPE_PNG ) {
   $target_file_name.=".png";  
   $image = imagecreatefrompng($filename);
  }
  
  // get width height of upload image file
  $height = imagesy($image);
  $width = imagesx($image);
 
  // SET THE SIZE OF TARGET IMAGE i use 250 x 250
  $target_width = 250;
  $target_height = 250;
  // Create a Empty Image Canvas
  $new_image = imagecreatetruecolor($target_width,$target_height);
  imagealphablending($new_image, true);
  imagesavealpha($new_image, true);
  
  // Set Canvas Background Color / Transparent as per your requirement
  // I prefer to go with Transparent
  
  // Make canvas Filled Background Color
  $white = imagecolorallocate($new_image, 255, 255, 255);
  $grey = imagecolorallocate($new_image, 128, 128, 128);
  $black = imagecolorallocate($new_image, 0, 0, 0);
  imagefilledrectangle($new_image, 0, 0, $target_width,$target_height, $grey); 
  
  // Make Canvas Transparent
  $trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
  imagefill($new_image, 0, 0, $trans_colour);
 
  // Copy Source Image to Canvas
  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $target_width,$target_height,$width, $height);
  $image = $new_image;
  

  
  // Save image based on file type;
  if( $image_type == IMAGETYPE_JPEG ) {
   imagejpeg($image,$filename,100);
  } elseif( $image_type == IMAGETYPE_GIF ) {
   imagegif($image,$target_file_name);         
  } elseif( $image_type == IMAGETYPE_PNG ) {
   imagepng($image,$target_file_name);
  }   
   chmod($target_file_name,777);
  
  $response = '<span style="color:#060">Image Successfully Uploaded!</span>';
 }else
 {
  $response = '<span style="color:#FF0000;">Select a Valid (JPG,GIF,PNG) Images</span>';
 }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload and Resize Image</title>
<style type="text/css">
body,td,th {
 font-size: 14px;
 font-family: Verdana, Geneva, sans-serif;
}
body {
 margin-left: 10px;
 margin-right: 10px;
}
</style>
</head>
<body>

<div style="padding:10px;">
<?php echo @$response; ?>
</div>
<form action="" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend>Upload Image</legend>
    
        <input name="image_file" type="file" id="image_file" />
        
        <input name="upload_image" type="submit" id="upload_image" value="Upload" />
        
    </fieldset>
</form>
</body>
</html>