上传的ZIP存档。在PHP中仅将PNG转换为JPEG

时间:2012-03-06 00:57:04

标签: php zip png jpeg

对于我的生活,我无法弄清楚如何编写这个过程的一部分:

我已经完成了这些步骤:
 1.上传ZIP存档(仅包含gif,png和jpg中的照片)
 2.打开包装到文件夹
 3.扫描文件夹以查找文件名+文件扩展名

我需要帮助:
 4.仅将PNG转换为JPG

任何帮助将不胜感激!

布兰登

编辑:
这有意义吗?

$directory = "../images/ilike/goldfish/";
$images = glob($directory . "*.jpg");

foreach($images as $image)  
{  
$pic = imagecreatefrompng($directory);  
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));  
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));  
imagealphablending($bg, TRUE); 
imagecopy($bg, $pic, 0, 0, 0, 0, imagesx($image), imagesy($image))  
imagedestroy($image);  
imagejpeg($bg, $image . ".jpg", 100);  
ImageDestroy($bg);  
}

2 个答案:

答案 0 :(得分:0)

像往常一样从表单中获取文件,使用http://www.php.net/manual/en/class.ziparchive.php打开它并使用http://www.php.net/manual/en/ziparchive.extractto.php将文件解压缩到空文件夹。然后使用PHP标准文件处理函数扫描以.jpg结尾的文件名。使用http://www.php.net/manual/en/function.imagecreatefromjpeg.php加载它们,然后使用http://www.php.net/manual/en/function.imagepng.php保存它们。

答案 1 :(得分:0)

//here we create directory
---------------------------- 
$new_folder=mkdir('C:\\wamp\\www\\TestImage\\uploads\\'.$folder_name, 0777, true);
$path="uploads\\".$folder_name."\\";

//uploading files
-----------------------------
$fileName = $_FILES["upload_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["upload_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = @$_FILES["upload_file"]["application/zip"];  // The type of file it is
$fileSize = $_FILES["upload_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["upload_file"]["error"]; // 0 = false | 1 = true
$kaboom = explode(".",$_FILES["upload_file"]["name"]); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension


if (!preg_match("/.(zip)$/i", $fileName) ) {
     // This condition is only if you wish to allow uploading of specific file types    
     //echo "ERROR: Your file was not .zip file";
     echo "Please select image files.Supported format are .Zip</br>"; 
     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
     #exit();
}

//unzip to specific location
--------------------------------
if(preg_match("/.(zip)$/i", $fileName))
{
$moveResult= move_uploaded_file($fileTmpLoc, $fileName);

if($moveResult == true)
{ 
     $zip = new ZipArchive;

     $res = $zip->open($fileName);

    if($res==TRUE)
        {  
            $zip->extractTo($path.$fileName);

            echo "<pre>";
            print_r($zip);


            $zip->close();
        } else {
         echo 'failed';
        }

}

unlink($fileName); // Remove the uploaded file from the PHP temp folder
//exit();
}

//仅将PNG转换为JPG

的功能
    $image = imagecreatefrompng($originalFile);
        imagejpeg($image, $outputFile, $quality);//$outputFile->define name of output file and  $quality is a number between 0 (best compression) and 100 (best quality)
        imagedestroy($image);

note->refer for <gdlib> http://www.php.net/manual/en/function.imagejpeg.php