Jump to content

Php and Gd Images Simple Manipulation


ghostrider.k9

Recommended Posts

Hi Ppl really need your help cos i am stuck here. I need to resize and image and merge it with another image in my folder. i need your help..

 

<?php

//Function for unidentified extensions of images

function open_image ($file) {

        $extension = strrchr($file, '.');

        $extension = strtolower($extension);

        switch($extension) {

case '.jpg':

case '.jpeg':

$im = @imagecreatefromjpeg($file);

break;

case '.gif':

$im = @imagecreatefromgif($file);

break;

default:

$im = false;

break;

        }

        return $im;

}

 

// Load image

$image = open_image('test.jpg');

if ($image === false) { die ('Unable to open image'); }

 

// Get original width and height

$width = imagesx($image);

$height = imagesy($image);

 

$aspect_ratio=round($height/$width,2);

 

header('Content-type: image/jpeg');

//Width is fixed for images where width is more than height

$new_width = 300;

//New Height

$new_height = round((300/$width)*$height,0);

//shrink the image with the new Height

$image_resized = imagecreatetruecolor($new_width, $new_height);

 

//Create the White Border

$horz = imagecreatefromjpeg("images/whiteborder.jpg");

//Create the Second Image

$vert = imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//Get the height of the resized Image

$height_resized = imagesy($vert);

//Merge the Pixels

imagecopymerge($horz, $vert, 1, 113-($height_resized/2), 0, 0, 298, $height_resized, 100);

//Create the Converted Image

imagejpeg($horz);

//Destroy The Images

imagedestroy($horz);

imagedestroy($vert);

?>

thanks..

 

Link to comment
https://forums.phpfreaks.com/topic/112894-php-and-gd-images-simple-manipulation/
Share on other sites

I got the solution but i need to save the result image into my folder How to??

 

<?php

header("Content-type: image/jpeg");

function resizeimg($image)

{

  $x = @getimagesize($image);

  $width = $x[0];

  $height = $x[1];

 

  $new_width = round((225/$height)*$width,0);

  $new_height = round((300/$width)*$height,0);

 

  $new = @ImageCreateFromJPEG($image) or $new = @ImageCreateFromPNG($image) or $new =

      @ImageCreateFromGIF($image) or $new = false;

 

  if(!$new)

  {

      readfile($new);

  }

  else

  {

      $thumb = @ImageCreateTrueColor($new_width, $new_height);

      @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

      return $thumb;

  }

  return false;

}

$image = resizeimg('test4.jpg');

$horz = imagecreatefromjpeg("images/whiteborder.jpg");

$height_resized = imagesy($image);

$width_resized = imagesx($image);

imagecopymerge($horz, $image, 150-($width_resized/2), 113-($height_resized/2), 0, 0, $width_resized, $height_resized, 100);

imagejpeg($horz);

imagedestroy($horz);

imagedestroy($vert);

 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.