PHP批量转换PNG转JPG源码

    <?php
    /*
    $saveDir 保存地址
    $file png图片地址

    */
    function imageToJpg($saveDir,$file){
    $saveDir = str_replace ( "\\", "/", $saveDir );
    $srcFile=$saveDir.$file;
    $srcFileExt= strtolower ( trim ( substr ( strrchr ( $srcFile, '.' ), 1 ) ) );
    if($srcFileExt=='jpeg'||$srcFileExt=='jpg'){
    return $saveDir.$file;
    }
    $srcFileName = basename ( $file ,'.'.$srcFileExt);
    $dstFile = $saveDir.$srcFileName.".jpg";
    $photoSize = GetImageSize($srcFile);
    $pw = $photoSize[0];
    $ph = $photoSize[1];
    $srcImage = true;
    if ( stripos( strtolower($srcFile),".gif") ){
    //创建图片
    $dstImage = ImageCreateTrueColor( $pw, $ph);
    imagecolorallocate($dstImage, 255, 255, 255);
    //读取图片
    $srcImage = ImageCreateFromGif($srcFile);
    //合拼图片
    imagecopyresampled($dstImage,$srcImage ,0,0,0,0,$pw,$ph ,$pw,$ph);
    //ImageCopyResized($dstImage,$srcImage,0,0,0,0,$pw,$ph,$pw,$ph);
    ImageJpeg($dstImage,$dstFile);
    imagedestroy($srcImage );
    }
    if ( stripos( strtolower($srcFile),".png") ){
    //创建图片
    $dstImage = ImageCreateTrueColor( $pw, $ph);
    imagecolorallocate($dstImage, 255, 255, 255);
    //读取图片
    $srcImage = ImageCreateFromPNG($srcFile);
    //合拼图片
    imagecopyresampled($dstImage,$srcImage ,0,0,0,0,$pw,$ph ,$pw,$ph);
    ImageJpeg($dstImage,$dstFile);
    imagedestroy($srcImage );
    }
    /*if ( $srcImage == false || stripos( strtolower($srcFile),".jpg") ){
    $srcImage = ImageCreateFromJPEG($srcFile);
    }*/
    if(is_file($srcFile)){
    @unlink($srcFile);
    }
    return $saveDir.$srcFileName.".jpg";
    }

    $snum=0;
    $lujingb="*.{png}";
    $arr=glob($lujingb,GLOB_BRACE);
    if(count($arr)>0){
    $j=count($arr);
    for($i=0;$i<$j;$i++){
    $snum++;
    echo $snum."-".$arr[$i]."转换jpg成功<br>";
    $saveDir = "";
    $file = $arr[$i];
    imageToJpg($saveDir,$file);
    }
    }

    ?>

    转载请注明:RAIN MAN » PHP批量转换PNG转JPG源码

    喜欢 0
标签:

还没有人抢沙发呢~