// resize the original image to size of editor
    $resizedImage = imagecreatetruecolor($imgW, $imgH);
    imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
    // rotate the rezized image
    $rotated_image = imagerotate($resizedImage, -$angle, 0);
    // find new width & height of rotated image
    $rotated_width = imagesx($rotated_image);
    $rotated_height = imagesy($rotated_image);
    // diff between rotated & original sizes
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;
    // crop rotated image to fit into original rezized rectangle
    $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
    imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
    imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
    // crop image into selected area
    $final_image = imagecreatetruecolor($cropW, $cropH);
    imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
    imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
    // finally output png image
    //imagepng($final_image, $output_filename.$type, $png_quality);
    $mysqlObject = new mysqlModule();
    $sessionObject = new sessionModule();
    $mysqlObject->commandDataBase('INSERT INTO `page_images` (`page`, `path`) VALUES ("' . $sessionObject->getPage() . '", "' . $filename . $type . '")');
    imagejpeg($final_image, $output_filename . $type, $jpeg_quality);
    $response = array("status" => 'success', "url" => $real_filename . $type);
}
$realImageName = str_replace('../../images/temp/', '', $imgUrl);
rename('../../images/page/original/' . $realImageName, '../../images/page/original/' . $filename . $type);
unlink($imgUrl);
print json_encode($response);
Esempio n. 2
0
function handleFileUpload()
{
    $mysqlObject = new mysqlModule();
    $sessionObject = new sessionModule();
    $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
    $temp = explode('.', $_FILES['file']['name']);
    $extension = end($temp);
    if (($_FILES['file']['type'] == 'image/jpeg' || $_FILES['file']['type'] == 'image/jpg' || $_FILES['file']['type'] == 'image/png') && $_FILES['file']['size'] < 2000000 && in_array($extension, $allowedExts)) {
        if ($_FILES['file']['error'] > 0) {
            echo 'Return Code: ' . $_FILES['file']['error'] . '<br>';
        } else {
            $date = new DateTime();
            $dateString = sha1(str_replace(' ', '', $date->format('Y-m-d H:i:s')));
            $fileending = $_FILES['file']['type'] == 'image/jpeg' || $_FILES['file']['type'] == 'image/jpg' ? '.jpg' : '.png';
            $filename = $dateString . $fileending;
            /*echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            		echo "Type: " . $_FILES['file']['type'] . "<br>";
            		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";*/
            $profileImageId = $mysqlObject->queryDataBase('SELECT id FROM users WHERE name = "' . $sessionObject->getUserName() . '"')[0]['id'];
            $mysqlObject->commandDataBase('INSERT INTO `profile_images` (`user`, `path`) VALUES ("' . $profileImageId . '", "' . $filename . '")');
            move_uploaded_file($_FILES['file']['tmp_name'], '../images/profile/' . $filename);
            echo 'Successfull upload!';
        }
    } else {
        echo 'Invalid file';
    }
}
} else {
    // resize the original image to size of editor
    $resizedImage = imagecreatetruecolor($imgW, $imgH);
    imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
    // rotate the rezized image
    $rotated_image = imagerotate($resizedImage, -$angle, 0);
    // find new width & height of rotated image
    $rotated_width = imagesx($rotated_image);
    $rotated_height = imagesy($rotated_image);
    // diff between rotated & original sizes
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;
    // crop rotated image to fit into original rezized rectangle
    $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
    imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
    imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
    // crop image into selected area
    $final_image = imagecreatetruecolor($cropW, $cropH);
    imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
    imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
    // finally output png image
    //imagepng($final_image, $output_filename.$type, $png_quality);
    $mysqlObject = new mysqlModule();
    $sessionObject = new sessionModule();
    $profileImageId = $mysqlObject->queryDataBase('SELECT id FROM users WHERE name = "' . $sessionObject->getUserName() . '"')[0]['id'];
    $mysqlObject->commandDataBase('INSERT INTO `story_images` (`story`, `path`) VALUES ("' . $sessionObject->getStory() . '", "' . $filename . $type . '")');
    imagejpeg($final_image, $output_filename . $type, $jpeg_quality);
    $response = array("status" => 'success', "url" => $real_filename . $type);
}
unlink($imgUrl);
print json_encode($response);
} else {
    // resize the original image to size of editor
    $resizedImage = imagecreatetruecolor($imgW, $imgH);
    imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
    // rotate the rezized image
    $rotated_image = imagerotate($resizedImage, -$angle, 0);
    // find new width & height of rotated image
    $rotated_width = imagesx($rotated_image);
    $rotated_height = imagesy($rotated_image);
    // diff between rotated & original sizes
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;
    // crop rotated image to fit into original rezized rectangle
    $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
    imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
    imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
    // crop image into selected area
    $final_image = imagecreatetruecolor($cropW, $cropH);
    imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
    imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
    // finally output png image
    //imagepng($final_image, $output_filename.$type, $png_quality);
    $mysqlObject = new mysqlModule();
    $sessionObject = new sessionModule();
    $profileImageId = $mysqlObject->queryDataBase('SELECT id FROM users WHERE name = "' . $sessionObject->getUserName() . '"')[0]['id'];
    $mysqlObject->commandDataBase('INSERT INTO `profile_images` (`user`, `path`) VALUES ("' . $profileImageId . '", "' . $filename . $type . '")');
    imagejpeg($final_image, $output_filename . $type, $jpeg_quality);
    $response = array("status" => 'success', "url" => $real_filename . $type);
}
unlink($imgUrl);
print json_encode($response);