Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    120
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how to convert tiff to jpg with php?

    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:
    PHP Code:
    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_imgoctdec($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$srcimg0000$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?

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    http://www.imagemagick.org/script/index.php

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

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    120
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yeah i have reading that site before but i couldn't figure how to use it

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,077
    Thanks
    11
    Thanked 98 Times in 96 Posts
    If imagemagic is installed on your server (most unix hosts have it) its as simple as ...

    PHP Code:
    <?php
        $exec 
    "convert /path/to/file.tiff /path/to/file.jpg";
        
    exec($exec$yaks);
        
    //to view any errors >> // print_r($yaks); 
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    <?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();
    }
    ?>

  • #6
    New Coder
    Join Date
    Feb 2007
    Posts
    86
    Thanks
    0
    Thanked 2 Times in 2 Posts
    If your using Imagick, you should use setformat(). This is basically how I'm using it on my site:

    PHP Code:
    <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>
    </select>
    <input type="submit">
    </form>
    <?php
    $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();
    $image->readImageBlob($blob);

    # set the format
    $image->setFormat($type);

    # write the file
    $image->writeimage("$file.$type");
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •