View Single Post
Old October 29th, 2017, 05:43 AM   #18
herkalurk
Vintage Member
 
herkalurk's Avatar
 
Join Date: Apr 2011
Location: Tulsa
Posts: 23,167
Thanks: 609
Thanked 275,073 Times in 23,047 Posts
herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+herkalurk 1000000+
Default

I actually wrote the gallery upload in php. I save the information to a mysql database so I know all of my galleries for later user. I tend to upload many at a time, so this makes it easier to recall later. This requires the unirest extra code as displayed in the tutorial at pixhosts's api.

The script works by passing 2 arguments: first is the name of the gallery you're creating, second is the folder where the images are located.

Code:
<?php

require_once '/home/user/unirest-php/src/Unirest.php';

$db=mysqli_connect('localhost','pix','pix','pix');

function create_gal($gal_name) {
        $response = Unirest\Request::post("https://api.pixhost.to/galleries",
                [
                        "Content-Type" => "application/x-www-form-urlencoded; charset=utf-8",
                        "Accept" => "application/json"
                ],
                Unirest\Request\Body::form([
                        "gallery_name" => $gal_name
                ])
        );
        print_r($response);
        return $response;
}

function finalize() {
        global $gal_json;
        $response = Unirest\Request::post("https://api.pixhost.to/galleries/{$gal_json->body->gallery_hash}/finalize",
                [
                        "Content-Type" => "application/x-www-form-urlencoded; charset=utf-8",
                        "Accept" => "application/json"
                ],
                Unirest\Request\Body::form([
                        "gallery_upload_hash" => "{$gal_json->body->gallery_upload_hash}"
                ])
        );
}

function upload($img) {
        global $gal_json;
        $response = Unirest\Request::post("https://api.pixhost.to/images",
                [
                        "Content-Type" => "multipart/form-data; charset=utf-8",
                        "Accept" => "application/json"
                ],
                [
                        "img" => Unirest\Request\Body::file($img),
                        "content_type" => "1",
                        "max_th_size" => "180",
                        "gallery_hash" => $gal_json->body->gallery_hash,
                        "gallery_upload_hash" => $gal_json->body->gallery_upload_hash
                ]
        );
        print_r($response);
        return $response;
}

//$gal_json=json_decode(create_gal($argv[1]));
$gal_json=create_gal($argv[1]);

mysqli_query($db,"insert into galleries (`gallery_name`,`gallery_hash`,`gallery_upload_hash`,`gallery_url`) values ('{$gal_json->body->gallery_name}','{$gal_json->body->gallery_hash}','{$gal_json->body->gallery_upload_hash}','{$gal_json->body->gallery_url}');");

foreach(scandir($argv[2]) as $entry) {
        if ($entry != "." && $entry != ".." && $entry != "upload_folder.php") {
                $img_json=upload("{$argv[2]}{$entry}");
                mysqli_query($db,"insert into images (`gallery_hash`,`name`,`show_url`,`th_url`) values ('{$gal_json->body->gallery_hash}','{$img_json->body->name}','{$img_json->body->show_url}','{$img_json->body->th_url}');");
        }
}

finalize();

mysqli_close($db);

?>
If anyone cares the mysql table structure is below

Code:
-- phpMyAdmin SQL Dump
-- version 4.4.15.10
-- https://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 29, 2017 at 01:43 AM
-- Server version: 5.5.56-MariaDB
-- PHP Version: 5.4.16

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `pix`
--

-- --------------------------------------------------------

--
-- Table structure for table `galleries`
--

CREATE TABLE IF NOT EXISTS `galleries` (
  `ID` int(8) NOT NULL,
  `gallery_name` varchar(100) NOT NULL,
  `gallery_hash` varchar(5) NOT NULL,
  `gallery_upload_hash` varchar(40) NOT NULL,
  `gallery_url` varchar(60) NOT NULL,
  `addTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `images`
--

CREATE TABLE IF NOT EXISTS `images` (
  `ID` int(8) NOT NULL,
  `gallery_hash` varchar(5) NOT NULL,
  `name` varchar(150) NOT NULL,
  `show_url` varchar(150) NOT NULL,
  `th_url` varchar(150) NOT NULL,
  `addTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `galleries`
--
ALTER TABLE `galleries`
  ADD PRIMARY KEY (`ID`) USING BTREE,
  ADD UNIQUE KEY `gallery_hash` (`gallery_hash`) USING BTREE,
  ADD KEY `addTime` (`addTime`);

--
-- Indexes for table `images`
--
ALTER TABLE `images`
  ADD PRIMARY KEY (`ID`),
  ADD KEY `gallery_hash` (`gallery_hash`),
  ADD KEY `addTime` (`addTime`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `galleries`
--
ALTER TABLE `galleries`
  MODIFY `ID` int(8) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `images`
--
ALTER TABLE `images`
  MODIFY `ID` int(8) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
__________________
My posts are scripted, sorry if the preview images aren't great, but the whole gallery is in the link
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


If I post any duplicates, please PM me and I will remove them
herkalurk is offline   Reply With Quote
The Following 3 Users Say Thank You to herkalurk For This Useful Post: