使用php将所有上传的图像(png,gif)保存为jpg

使用php将所有上传的图像(png,gif)保存为jpg

问题描述:

How i can save all uploaded images as jpg using php. i have code php to upload images like this

 $upload_dir="../uploads/";
 $filename = $_FILES['pic1']['name'];    
 $tmp_name=$_FILES['pic1']['tmp_name'];
 $path=$upload_dir.$filename;
 move_uploaded_file($tmp_name, $path);

i am using

     $image_path=imagecreatefromjpeg($path); 
     `imagejpeg($image_path);`  

but not work!.

如何使用php将所有上传的图像保存为jpg。 i有代码php来上传这样的图像 p>

  $ upload_dir =“../ uploads /”; 
 $ filename = $ _FILES ['pic1'] ['name  “];  
 $ tmp_name = $ _ FILES ['pic1'] ['tmp_name']; 
 $ path = $ upload_dir。$ filename; 
 move_uploaded_file($ tmp_name,$ path); 
  code>  pre>  
 
 

我正在使用 p>

  $ image_path = imagecreatefromjpeg($ path);  
`imagejpeg($ image_path);`
  code>  pre> 
 
 

但不能正常工作! p> div>

This won't work because you tell PHP to create image from jpeg format and your uploaded file is gif or png. You could use imagecreatefromstring() function:

$image_path = imagecreatefromstring(file_get_contents($path));
imagejpeg($image_path);