Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // user uploads file and it moves to server
- move_uploaded_file ($_FILES['Image']['tmp_name'], "/sites/".$_FILES['Image']['name']) or die ('Could not upload');
- ?>
- <?php
- // create image path
- $img = imagecreatefromjpeg("/sites/".$_FILES['Image']['name']);
- ?>
- <?php
- //pixelate function
- //if($img && imagefilter($img,IMG_FILTER_PIXELATE,4))
- imagejpeg($img,'image.jpg',100);
- ?>
- <?php
- // resize image
- $output_width =30;
- $output_height=50;
- $xytotal = $output_width*$output_height;
- $path = '/sites/image.jpg';
- $size_arr = getimagesize($path);
- if ($size_arr[2]==IMAGETYPE_GIF)
- $image = imagecreatefromgif($path);
- else if ($size_arr[2]==IMAGETYPE_JPEG)
- $image = imagecreatefromjpeg($path);
- else if ($size_arr[2]==IMAGETYPE_PNG)
- $image = imagecreatefrompng($path);
- else
- die_default_image();
- $tmpname = tempnam( sys_get_temp_dir() , "phptmp");
- resize_image($tmpname, $image, $size_arr, $output_width, $output_height);
- $img = imagecreatefromjpeg($tmpname);
- imagejpeg($img,'image.jpg',100);
- unlink( $tmpname );
- // loop to populate rgb values into css spans
- $imagew = imagesx($img);
- $imageh = imagesy($img);
- echo "Image (w,h): ($imagew, $imageh)<br/>";
- $x = 0;
- $y = 0;
- $counter = 0;
- for ($y = 0; $y < $imageh; $y++) {
- for ($x = 0; $x < $imagew; $x++) {
- $rgb = imagecolorat($img, $x, $y);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $original_r = $r;
- $original_g = $g;
- $original_b = $b;
- if($r%16 !=0){ //if remainder of $r value is !=0 then increment $r value by 1 while $counter < 8
- do{
- $r++;
- $counter++;
- } //end do
- while($counter < 8);
- }
- elseif($r%16 != 0){
- $counter = 0;
- do{
- $r -=1;
- $counter++;
- } while($counter < 8);
- }
- $counter = 0; // reset counter value
- if($g%16 !=0){ //if remainder of $g value is !=0 then increment $r value by 1 while $counter < 8
- do{
- $g++;
- $counter++;
- } while($counter < 8);
- }
- elseif($g%16 != 0){
- $counter = 0;
- do{
- $g -=1;
- $counter++;
- } while($counter < 8);
- }
- $counter = 0; // reset counter value
- if($b%16 !=0){ //if remainder of $b value is !=0 then increment $r value by 1 while $counter < 8
- do{
- $b++;
- $counter++;
- } while($counter < 8);
- }
- elseif($b%16 != 0){
- $counter = 0;
- do{
- $b -=1;
- $counter++;
- } while($counter < 8);
- }
- // placing $rgb values into arrays to be shown
- $css_color = str_pad(dechex($r), 2, "0", STR_PAD_LEFT).str_pad(dechex($g), 2, "0", STR_PAD_LEFT).str_pad(dechex($b), 2, "0", STR_PAD_LEFT); //convert rgb into css
- //echo "[($x,$y) ($r,$g,$b)]";
- echo '<span style="width: 5px; height: 5px; background-color:#'.$css_color.'" >'.$x. ' ' .$y.' '.$css_color.' '.$r.' '.$g.' '.$b.'</span>';
- echo '<br />';
- $css_color_original = str_pad(dechex($r), 2, "0", STR_PAD_LEFT).str_pad(dechex($g), 2, "0", STR_PAD_LEFT).str_pad(dechex($b), 2, "0", STR_PAD_LEFT); // original css color values
- echo '<span style="width: 5px; height: 5px; background-color:#'.$css_color_original.'">'.$x. ' ' .$y.' '.$css_color_original.' '.$original_r.' '.$original_g.' '.$original_b.'<br /><br /></span';
- echo '<br />';
- }
- }
- ?>
- <script type="text/javascript" src="jquery-1.6.2.js"></script>
- <script src="formatted_colors.js" type="text/javascript" charset="utf-8"></script>
- <script type="text/javascript">
- //iterate through pixels and match rounded color to array in formatted_colors.js
- <div class="rounded_color" hex="<?php $css_color ?>" xy="<?php $x.$y ?>"</div>
- <div id="<?php $x.$y ?>"></div>
- $(document).ready(function() {
- $(".rounded_color").each(function(){
- var google_color = getColor($(this).attr("hex"));
- $('#'+$(this).attr("id")).html(google_color);
- $('#'+$(this).attr("id")).css('background-color:', google_color);
- })
- // get name of color function from formatted_colors.js
- function getColor(target_color){
- if (colors[target_color] == undefined) { // not found
- return "no match";
- } else {
- return colors[target_color];
- }
- } // end getColor function
- )} // end ready function
- </script>
- <?php
- //image resizing functions
- function die_default_image()
- {
- //43byte 1x1 transparent pixel gif
- header("content-type: image/gif");
- echo base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
- }
- function resize_image($thumbname, $image, $size_arr, $max_width, $max_height)//maintain aspect ratio
- {
- $width = $max_width;
- $height = $max_height;
- list($width_orig, $height_orig, $img_type) = $size_arr;
- $ratio_orig = $width_orig/$height_orig;
- if ($width/$height > $ratio_orig) {
- $width = floor($height*$ratio_orig);
- } else {
- $height = floor($width/$ratio_orig);
- }
- // Resample
- $tempimg = imagecreatetruecolor($width, $height);
- imagecopyresampled($tempimg, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
- imagejpeg($tempimg, $thumbname, 80);
- }
- if ( !function_exists('sys_get_temp_dir')) {
- function sys_get_temp_dir() {
- if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
- if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
- if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
- $tempfile=tempnam(uniqid(rand(),TRUE),'');
- if (file_exists($tempfile)) {
- unlink($tempfile);
- return realpath(dirname($tempfile));
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement