Jump to content

how to convert tiff to jpg with php?


alapimba

Recommended Posts

Hello

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);
}else{
$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;
imagedestroy($destimg);
$fh = fopen($prod_img, 'w') or die("can't open file");
fclose($fh);
unlink($prod_img);
$dir = $uploadfile;
}
}

anyone can help me?

Link to comment
Share on other sites

As far as I'm aware, the GDLib doesn't support conversions from/to TIFF formats. You'll need some third party tool installed on the server, such as ImageMagick. Converting with ImageMagick is simple:

 

<?php
exec('convert path/to/original/image.tiff path/to/new/image.jpg');
?>

Link to comment
Share on other sites

  • 4 years later...

Following code allows you to convert a multi-page TIFF file to JPG thumbnail. This conversion is usually required in those situations where you have to allow users to view a Fax document on a webpage. Since web browsers usually doesn’t support TIFF extensions. So you have to convert these documents to either JPG or PDF. Following code works for both situations. you just need to change jgp with pdf.

 

Following code is taken from sourcecodemania.com

 

<?php
try
{
  // 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
    $image->thumbnailImage(768,0);
    $image->writeImage("page".$i.".jpg");
    echo "<img src='page$i.jpg' alt='images' ></img>";
  }
  $images->clear();
}
catch(Exception $e)
{
  echo $e->getMessage();
}
?>

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.