Автоматично конвертиране

bganonymous

Registered
Имам скрипт за качване на снимки във формат .jpg, .jpeg, .png, .gif и .bmp.

Исками се когато някой качи снимка, тя автоматично да се конвертира в .jpg формат, независимо какъв формат е била първоначално.
 
bganonymous каза:
Имам скрипт за качване на снимки във формат .jpg, .jpeg, .png, .gif и .bmp.

Исками се когато някой качи снимка, тя автоматично да се конвертира в .jpg формат, независимо какъв формат е била първоначално.
Трябва да използваш GD библиотеката.
Варянт 1:
PHP:
function convertImage($originalImage, $outputImage, $quality)
{
    // jpg, png, gif or bmp?
    $exploded = explode('.',$originalImage);
    $ext = $exploded[count($exploded) - 1]; 

    if (preg_match('/jpg|jpeg/i',$ext))
        $imageTmp=imagecreatefromjpeg($originalImage);
    else if (preg_match('/png/i',$ext))
        $imageTmp=imagecreatefrompng($originalImage);
    else if (preg_match('/gif/i',$ext))
        $imageTmp=imagecreatefromgif($originalImage);
    else if (preg_match('/bmp/i',$ext))
        $imageTmp=imagecreatefrombmp($originalImage);
    else
        return 0;

    // quality is a value from 0 (worst) to 100 (best)
    imagejpeg($imageTmp, $outputImage, $quality);
    imagedestroy($imageTmp);

    return 1;
}

Варянт 2:
PHP:
<?php

$path = "uploads/";

$img = $_FILES['photoimg']['tmp_name'];
$dst = $path . $_FILES['photoimg']['name'];

if (($img_info = getimagesize($img)) === FALSE)
  die("Image not found or not an image");

$width = $img_info[0];
$height = $img_info[1];

switch ($img_info[2]) {
  case IMAGETYPE_GIF  : $src = imagecreatefromgif($img);  break;
  case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
  case IMAGETYPE_PNG  : $src = imagecreatefrompng($img);  break;
  default : die("Unknown filetype");
}

$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($tmp, $dst.".jpg");

?>

Варянт 3:
PHP:
if ($_FILES["photoimg"]["type"] == "image/jpeg" || $_FILES["photoimg"]["type"] == "image/png" || $_FILES["photoimg"]["type"] == "image/gif" )
    {
      if ($_FILES["photoimg"]["type"] == "image/png" || $_FILES["photoimg"]["type"] == "image/gif" )
      {
       imagejpeg(imagecreatefromstring(file_get_contents($_FILES["photoimg"]["tmp_name"])), "converted.jpg");
       $actual_image_name = time().$session_id.".".$ext;
       move_uploaded_file("converted.jpg", $path.$actual_image_name);
      }
      else
      {
        $actual_image_name = time().$session_id.".".$ext;
        $tmp = $_FILES['photoimg']['tmp_name'];
        move_uploaded_file($tmp, $path.$actual_image_name);
      }
    }

Просто се налага да използваш точно GD.

http://bg2.php.net/manual/bg/book.image.php
Тук има достатъчно много примери.
Eто и един пример с форма.
http://www.codingforums.com/php/174961-convert-all-image-types-jpg.html
Код:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Image Type Convertor</title>
</head>

<body>

<form action="convertor1.php" method="post" enctype="multipart/form-data">
    <input type="file" name="img" />
    <input type="submit" name="submit" value="submit" />
</form>

<?php

// Check if form is submitted
if (isset($_FILES["img"]))
{
    // Use a shorter variable name for ease of typing.
    $name = $_FILES["img"]["name"];


    // Determine file's MIME type and create appropriate object.
    switch ($_FILES["img"]["type"])
    {
        // If image doesn't need to be converted
        case "image/jpeg":
            move_uploaded_file($_FILES["img"]["tmp_name"], "/tmp/" . $name . ".jpg");
            // I did put the .jpg extension because MIME type can be JPEG and extension can be something else and this helps you put the image in web page safely.
            break;


        // PNG for example
        case "image/png":
            $img = imagecreatefrompng($_FILES["img"]["tmp_name"]);
            break;


        case "Put other MIME type here":
            //$img = // appropriate function here. GD supports png, gif, bmp, gd, etc.
            break;


        default:
            // Unsupported MIME type
            break;
    }



    // Create the image
    imagejpeg($img, "tmp/" . $name . ".jpg"); // Notice that the resulting file name will be something like image.png.jpg which doesn't make any problem at all.


    // Unload the image object from server RAM.
    // This can also be automatically done when php engine reaches end of page.
    // But it's a long story to tell you how this helps server.
    imagedestroy($img);
}
?>


</body>
</html>
 

Back
Горе