0

我有两个脚本,一个要求我的图片是 png,但我的上传者将它创建为 jpeg。如果我尝试对其进行编辑以使其成为将图像转换为 png 的位置,它会将其作为纯 blak 文件上传,并且它仍然是 jpeg。谁能帮我弄清楚我做错了什么?

这是我的上传代码的一部分:(我试图让它们既是 imagecreatefrompng() 又是 imagepng(),但仍然无法工作)

$file_name = $pic;
    $info = pathinfo($pic);
    $img = imagecreatefromjpeg( $pic );
    $width = imagesx( $img );
    $height = imagesy( $img );


    if ($_POST['RadioGroup1'] == 'top') {
    $new_width = 400; //New top width
    $new_height = 800; //New top height
        ;} else {
    $new_width = 800; //New side width
    $new_height = 400; //New side height
        ;}


    $pic = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $pic, $img, 0, 0, 0, 0, $new_width,       $new_height, $width, $height );
    imagejpeg( $pic, "{$resize}{$file_name}" );

或者也许答案就在我的 zend 上传文件中。

if (isset($_POST['upload'])) {
require_once('scripts/library.php');
//try {
$destination = '----';

$uploader = new Zend_File_Transfer_Adapter_Http();
$uploader->setDestination($destination);
$filename = $uploader->getFileName(NULL, FALSE);
$uploader->addValidator('Size', FALSE, '10000kB');
$uploader->addValidator('ImageSize', FALSE, array('minheight' => 100, 'minwidth' => 100));
//$pic = $filename;
if (!$uploader->isValid() || $errors) {
    $messages = $uploader->getMessages();
} else {
//$pic = $filename;
$no_spaces = str_replace(' ', '_', $filename, $renamed);
$uploader->addValidator('Extension', FALSE, 'gif, png, jpg');
$recognized = FALSE;
if ($uploader->isValid()) {
    $recognized = TRUE;
} else {
    $mime = $uploader->getMimeType();
    $acceptable = array('jpg' => 'image/jpeg',
                        'png' => 'image/png',
                        'gif' => 'image/gif');
    $key = array_search($mime, $acceptable);
    if (!$key) {
        $messages[] = 'Unrecognized image type';
    } else {
        $no_spaces = "$no_spaces.$key";
        $recognized = TRUE;
        $renamed = TRUE;
    }
}
$uploader->clearValidators();
if ($recognized) {
    $existing = scandir($destination);
    if (in_array($no_spaces, $existing)) {
        $dot = strrpos($no_spaces, '.');
        $base = substr($no_spaces, 0, $dot);
        $extension = substr($no_spaces, $dot);
        $i = 1;
        do {
            $no_spaces = $base . '_' . $i++ . $extension;
        } while (in_array($no_spaces, $existing));
        $renamed = TRUE;
    }
$uploader->addFilter('Rename', array('target' => $no_spaces));
$success = $uploader->receive();
if (!$success) {
$messages = $uploader->getMessages();
} else {
    //$pic = $no_spaces;
    $uploaded = "$filename uploaded successfully";
    $pic = $filename;
    if ($renamed) {
        $pic = "pictures/uploads/" . $no_spaces;
        $uploaded .= " and renamed $no_spaces";
        //$pic = $no_spaces;
        //$pic = $uploader->getFileName(NULL, FALSE);

    } else {$pic = "pictures/uploads/" . $filename;;}
    $messages[] = "$uploaded";
    //$pic = $no_spaces;
4

0 回答 0