Jump to content

Convert images to .jpg


acctman

Recommended Posts

hi can someone assist me with the proper coding and placement for converting images to .jpg. Also if a check can be done to see if the image is already .jpg then don't convert but if the image is .png .bmp .jpeg .bmp .gif

 

thanks in advance

 

<?php 

$styles = '<link type="text/css" rel="stylesheet" href="css/imig.css" media="screen, projection" />';

include 'header.php';

if(isset($_POST['imageFileName'])) { 

  // DEFINE VARIABLES
  $imageWidth      = $_POST['imageWidth'];
  $imageFileName   = $_POST['imageFileName'];
  $resizeWidth     = $_POST['resizeWidth'];
  $sourceFile      = "working/cropped/". $imageFileName;
  $destinationFile = "working/finished/" . $imageFileName;

  if(file_exists($destinationFile)) { chmod($destinationFile, 0777); unlink($destinationFile); }  // delete if existing

  // CHECK TO SEE IF WE NEED TO CROP
  if($imageWidth != $resizeWidth) {
    $convertString = "convert $sourceFile -resize $resizeWidth $destinationFile";
    exec($convertString);
    chmod($destinationFile, 0777);
    chmod($sourceFile, 0777);
    unlink($sourceFile);

  } else { // RESIZE WAS SKIPPED
    $cmd = "mv " . $sourceFile . " " . $destinationFile;
    exec($cmd);
  }

?>

  <div class="info">
    <h1>All Done</h1>
    <p>Your image has been cropped and resized as you'd like.  The image below can be used as you see fit.</p>
  </div>


<div id="completedImage">
  <?php
  echo "<img src=\"" . $destinationFile . "\" id=\"theImage\" alt=\"Final Image\" />";
  $workingDir = exec('pwd');
  $finalFile = $workingDir . "/" . $destinationFile;
  // echo "Final File Location: " . $finalFile;
  ?>
</div> <!-- completedImage -->

<?php } else { ?> 

  <div class="info">
    <h1>Error</h1>
    <p>There was an error.</p> 
  </div>

<?php } include 'footer.php' ?>

Link to comment
https://forums.phpfreaks.com/topic/80686-convert-images-to-jpg/
Share on other sites

How do you wanna do it? With PHP (gdlib functions) or Linux/Unix (using exec)?

 

with imagemagick (www.imagemagick.org/script/convert.php) thats what i've been using to edit the resizing. linux method would be fine to which ever converts without making the file huge.

Link to comment
https://forums.phpfreaks.com/topic/80686-convert-images-to-jpg/#findComment-409253
Share on other sites

Using GD you could do some like the following. (haven't tested it).

 

<?php

$extension = explode(".", $filename); 
$extension = $extension[count($extension)-1];

$extension = strtolower($extension); 

if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg") {
          
          $image = imagecreatefromjpeg($picture); 
          
            } elseif($extension == "png") {        
          
          $image = imagecreatefrompng($picture); 
          
            } elseif($extension == "gif") {
            
          $image = imagecreatefromgif($picture); 
            
            }  
?> //only put this tag in for syntax highlighting..

 

Use your code for trimming and resizing, perhaps.

 

Then to save the file

 

<?php //only put this tag in for syntax highlighting..
header ("Content-type : image/jpg");
ImageJpeg($image);
ImageJpeg($image, "image.jpg");
ImageDestroy($image);
<?

 

Although I wouldn't recommend converting to jpg, though.. When converted from png, even though it's a lot smaller, the quality is pretty shocking.. so gif would be a better one.

 

Hope that helps a bit

 

Sam

Link to comment
https://forums.phpfreaks.com/topic/80686-convert-images-to-jpg/#findComment-409295
Share on other sites

i keep getting an error on this part

ImageJpeg($image);
ImageJpeg($image, "image.jpg");
ImageDestroy($image);

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/rmb/public_html/testcrop/slide/last_step.php on line 35

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/rmb/public_html/testcrop/slide/last_step.php on line 36

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/rmb/public_html/testcrop/slide/last_step.php on line 37

Link to comment
https://forums.phpfreaks.com/topic/80686-convert-images-to-jpg/#findComment-409504
Share on other sites

i keep getting an error on this part

ImageJpeg($image);
ImageJpeg($image, "image.jpg");
ImageDestroy($image);

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/rmb/public_html/testcrop/slide/last_step.php on line 35

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/rmb/public_html/testcrop/slide/last_step.php on line 36

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/rmb/public_html/testcrop/slide/last_step.php on line 37

 

I think you, in your part of the code, need to refrence $image more, or change $image in what I gave you to $destinationFile (or source file, depending on which is the first).

 

Also I think you need the image production code in a seperate file to the HTML, then reference to it after the HTML in another document with

<img src='last_step.php'>

 

Hope that helps

 

Sam

Link to comment
https://forums.phpfreaks.com/topic/80686-convert-images-to-jpg/#findComment-409619
Share on other sites

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.