IT Community - Software Programming, Web Development and Technical Support

Image handling in php

This is a discussion on Image handling in php within the PHP Programming forums, part of the Web Development category; hi How to create jpeg image in PHP....


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-06-2007, 03:01 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Thumbs up Image handling in php

hi
How to create jpeg image in PHP.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 03:24 AM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: Image handling in php

Hi
using GD library we can create jpeg image in PHP
Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-06-2007, 04:50 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Default Re: Image handling in php

Can you able to give me the sample for create jpeg using GD library ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-06-2007, 05:16 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Image handling in php

Hi Varghese,
Please view this link it can help to know more to create thumbnail whatever it is either jpeg or gif
Article - How to create thumbnails with PHP and gd.

For simple we can

header("Content-type: image/jpeg");
$im = @imagecreate(100, 50)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagejpeg($im);
imagedestroy($im);

i think it will help you
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-06-2007, 06:34 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: Image handling in php

You can create thumbnail image with the use of imagecopyresampled method. This is one GD image library method. In this method just mention the source and destination location with specific width and height. With this you can create thumbnail of any image. Sample is as follow.

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-06-2007, 11:47 PM
Anand Anand is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 52
Anand is on a distinguished road
Default Re: Image handling in php

Varghese,

You can also use Imagemagick library for image manipulations. Imagemagick is much faster with better quality compared to GD library.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-07-2007, 06:28 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Thumbs up Re: Image handling in php

Hi Jeyaseelan & venkat_charya,
The above examples shows the image in the browser. Actually i want to save the image into a specific location. How to do this.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-07-2007, 07:42 AM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: Image handling in php

Hi

Add the destination path in output

for example if you want to store the image in the same location then


<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, $filename, 100);
?>

The above code will save the image in jpg format with the file name 'test.jpg'.

Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-08-2007, 02:02 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: Image handling in php

Varghese

In ImageJPEG function we just have to remove the null parameter and put the destination path. As i stated in bold red color, just put the destination path in that variable it will store the image in that specified location.

imagejpeg($image_p, $filename, 100);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-08-2007, 02:04 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Question Re: Image handling in php

Anand,

Can you tell me which magick function we can use for image creation and how we can say it is much faster than GD?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 08-08-2007, 08:00 AM
Anand Anand is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 52
Anand is on a distinguished road
Default Re: Image handling in php

Creating images with Imagemagick is very easy.

<?
$ImgPath = "test.jpg";
$newWidth = 150;
$newHeight = 150;

//# -- Reads the jpg image
$ImgHandler = Imagick_readimage($ImgPath);

//#-- function used to resize the image
Imagick_sample($ImgHandler, $newWidth, $newHeight ,"100x100");

$DestImgPath = "newimage.jpg";
//#-- Writes the image as newimage.jpg
Imagick_writeimages($ImgHandler,$DestImgPath);
?>

The above code will reads an image and saves into a specific location with a new name.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 08-09-2007, 04:11 AM
Anand Anand is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 52
Anand is on a distinguished road
Default Re: Image handling in php

Imagemagick creates the images much faster when compared to GD library. Also GD creates the image with 72 DPI only, where as Imagemagick holds the actual DPI of the original image to the newly created images. Thus Imagemagick is much better than GD library.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 08-09-2007, 07:37 AM
kathir kathir is offline
D-Web Trainee
 
Join Date: Feb 2007
Posts: 25
kathir is on a distinguished road
Default Re: Image handling in php

hi Anand,
can you give a simple example program, how to create a image by using Imagemagic and how faster then GD.
Thanks
-Kathir
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 08-11-2007, 06:34 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: Image handling in php

Anand

I checked your code and it is working good. Actually GD library creates image with 96 DPI resolution not 72 DPI.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 08-11-2007, 06:36 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: Image handling in php

Kathir

As above anand specified you can create image with the help of Imagick read and write.

<?
//Specify image name and set desired height and width
$ImgPath = "test.jpg";
$newWidth = 150;
$newHeight = 150;

//# -- Reads the jpg image
$ImgHandler = Imagick_readimage($ImgPath);

//#-- function used to resize the image
Imagick_sample($ImgHandler, $newWidth, $newHeight ,"100x100");

$DestImgPath = "newimage.jpg";
//#-- Writes the image as newimage.jpg
Imagick_writeimages($ImgHandler,$DestImgPath);
?>

I used this and found it is much faster than GD and also the created image resolution having same DPI like the original image. This is not possible in GD.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 08-11-2007, 06:44 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: Image handling in php

I checked with sample code and found magick creates image with same resolution like original image but Can anybody tell me how we can set our desired DPI for new creation image?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 08-13-2007, 08:33 AM
Anand Anand is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 52
Anand is on a distinguished road
Default Re: Image handling in php

venkat_charya,

you can set the resolution of the image by using imagick_setdpi function.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 08-13-2007, 10:39 PM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Question Re: Image handling in php

Anand,

Can you please provide one sample about how to use that function?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 08-13-2007, 11:20 PM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: Image handling in php

Hi KK

use the below sample to change the image DPI
<?
//Specify image name and set desired height and width
$ImgPath = "test.jpg";
$newWidth = 150;
$newHeight = 150;
$horizontalDPI = 400;
$verticalDPI = 400;

//# -- Reads the jpg image
$ImgHandler = Imagick_readimage($ImgPath);

//#-- function used to resize the image
Imagick_sample($ImgHandler, $newWidth, $newHeight ,"100x100");

$DestImgPath = "newimage.jpg";

//#-- set the image Horizontal and Vertical dots per inch
Imagick_setdpi ( $ImgHandler, $horizontalDPI,$verticalDPI )

//#-- Writes the image as newimage.jpg
Imagick_writeimages($ImgHandler,$DestImgPath);
?>

Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 08-18-2007, 08:57 AM
Naseema Naseema is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 12
Naseema is on a distinguished road
Default Re: Image handling in php

Hi Anand
as kk say..
it will be really aapreciatable if u give more information..
thanx in advance
__________________
Nasee
True Faith never Fails
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/php-programming/3146-image-handling-php.html
Posted By For Type Date
Anandkv's bookmarks on del.icio.us This thread Refback 08-21-2007 06:32 AM
DiscussWeb IT Community - Fusing This thread Refback 08-06-2007 03:07 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert image to other image format using CODEC in .NET 3.0 Mramesh C# Programming 0 02-07-2008 03:33 AM
How to create an image from panel background Image S.Vinothkumar C# Programming 1 10-22-2007 03:52 AM
Error Handling in PHP sivaramakrishnan PHP Programming 13 09-21-2007 05:03 AM
How can I do error handling in php? itbarota PHP Programming 0 09-12-2007 12:39 AM
Image Handling in PERL raj Perl 4 08-23-2007 12:23 AM


All times are GMT -7. The time now is 06:29 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0