将文本应用于PHP中的图像

时间:2017-03-25 22:50:56

标签: php

好吧所以我正在尝试将一些文字应用到我上传到我网站的图片上。我基本上保存了它的正常版本+编辑过的版本。以下是问题

  • 原始图片会放大,因此只有50%可见
  • 应该编辑的图片版本根本没有保存,我得到的只是预览...
  • 我想将编辑后的图片大小设置为100x100px,但我不知道如何

我正在使用的代码:

<?php
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    require_once "assets/core/user.php";

    use app\user\control as user_c;


    $target_dir = "assets/img/user_uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $custom_name = substr(hash('sha512',$_SESSION['username']."_".time()),10,20);
    $fileNAME = basename($_FILES["fileToUpload"]["name"]); 

    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 1500000) {
        echo "Fajl je preveliki";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "<br/>Sorry, your file was not uploaded. "."FILE NAME: ".$fileNAME."<br/> FILE TYPE: ".$imageFileType." <br/> TARGET: ".$target_file;
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]['tmp_name'], "assets/img/user_uploads/".$custom_name.".".$imageFileType)) {

            //Just adding stuff to MySQL
            $user = new user_c();

            if ($_POST['customFileText2'] == "") {
                $user->submitUserUpload($_SESSION['username'], $custom_name, $_POST['customFileText']." ".$_POST['customFileText2'], $_POST['selectVal'], $imageFileType);
            } else {
               $user->submitUserUpload($_SESSION['username'], $custom_name, $_POST['customFileText']."_LINEBREAK_".$_POST['customFileText2'], $_POST['selectVal'], $imageFileType); 
            }


            //CREATING EDITED IMAGE

            $file_formated = "assets/img/user_uploads/".$custom_name.".".$imageFileType;

            $stamp = imagecreatefromfile($file_formated);

            $im = imagecreatefrompng('demo.png');

            $marge_right = 10;
            $marge_bottom = 10;
            $sx = imagesx($stamp);
            $sy = imagesy($stamp);

            imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

            // Output and free memory
            header('Content-type: image/png');
            imagepng($im);
            imagedestroy($im);

            header("Location: index?id=".$custom_name."");

        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }

    function imagecreatefromfile( $filename ) {
        if (!file_exists($filename)) {
            throw new InvalidArgumentException('File "'.$filename.'" not found.');
        }
        switch ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ))) {
            case 'jpeg':
            case 'jpg':
                return imagecreatefromjpeg($filename);
            break;

            case 'png':
                return imagecreatefrompng($filename);
            break;

            case 'gif':
                return imagecreatefromgif($filename);
            break;

            default:
                throw new InvalidArgumentException('File "'.$filename.'" is not valid jpg, png or gif image.');
            break;
    }
}
?>

0 个答案:

没有答案