View Full Version : how to convert tiff to jpg with php?

11-15-2006, 11:27 AM
I'm building a backend to allow a client to upload images to his site.
But my client has a lot of images in tiff format so i'd like to be able to convert tiff to jpg and only after that upload the image.
i'm using the command move_uploaded_file to upload the original image and then i'm resizing the image and creating a new one and delete the original. now i want to add to my script a way to convert the original tiff to jpeg, resize save and delete the original

here is my code that it's working fine:

if ($_FILES['foto']['name'] == "") {
$dir = "images/spacer.gif";
} else {
$size = 124; // the thumbnail height
$filedir = 'img/ambientes/'; // the directory for the original image
$thumbdir = 'img/ambientes/'; // the directory for the thumbnail image
$prefix = 'small_'; // the prefix to be added to the original name
$maxfile = '200000';
$mode = '0666';
$userfile_name = $_FILES['foto']['name'];
$userfile_tmp = $_FILES['foto']['tmp_name'];
$userfile_size = $_FILES['foto']['size'];
$userfile_type = $_FILES['foto']['type'];
if (isset($_FILES['foto']['name']))
$prod_img = $filedir.$userfile_name;
$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod ($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
$aspect_ratio = $sizes[0]/$sizes[1];
if ($sizes[0] <= $size)
$new_width = $size;
$new_height = abs($new_width*$aspect_ratio);
$new_width = $size;
$new_height = abs($new_width/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling');
ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
$uploadfile = $prod_img_thumb;
$fh = fopen($prod_img, 'w') or die("can't open file");
$dir = $uploadfile;

anyone can help me?

SeeIT Solutions
11-15-2006, 11:56 AM

I've never used it, but it does what you want.

11-15-2006, 12:37 PM
yeah i have reading that site before but i couldn't figure how to use it :confused:

11-16-2006, 01:28 AM
If imagemagic is installed on your server (most unix hosts have it) its as simple as ...

$exec = "convert /path/to/file.tiff /path/to/file.jpg";
exec($exec, $yaks);
//to view any errors >> // print_r($yaks);

10-19-2011, 06:02 PM
Using ImageMagick you can convert your tiff (or even multi page tiff) files to jpg in php. Following is a code for multipage tiff file conversion to jpg in PHP.
Following code is taken from sourcecodemania.com (http://sourcecodemania.com/converting-multipage-tiff-file-to-jpggif-in-php/)

// Saving every page of a TIFF separately as a JPG thumbnail
$images = new Imagick("testing.tif");
foreach($images as $i=>$image) {
// Providing 0 forces thumbnail Image to maintain aspect ratio
echo "<img src='page$i.jpg' alt='images' ></img>";
catch(Exception $e)
echo $e->getMessage();

10-20-2011, 11:56 PM
If your using Imagick, you should use setformat(). This is basically how I'm using it on my site:

<form action="" method="post">
url: <input type="text" name="url" value="">
<select name="type">
<option value="jpeg">JPG</option>
<option value="gif">GIF</option>
<option value="png">PNG</option>
<option value="bmp">BMP</option>
<option value="ico">ICO</option>
<option value="tiff">TIFF</option>
<input type="submit">
$url = $_POST[url];

#get file name
$extract = pathinfo($url);
$file = ($extract['filename']);

# read image as a blob instead of from a file path
$blob = file_get_contents($url);
$image = new imagick();

# set the format

# write the file

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum