Skip to content
Browse files

commit 49 awesome way of selecting a logo for a link

  • Loading branch information...
1 parent a25df9c commit e437104125c3f9c669671ceb773d96a8408d5b6a @phpfreak committed Sep 30, 2011
View
41 application/plugins/links/controllers/LinksController.class.php
@@ -203,11 +203,40 @@ function edit_logo() {
if (is_array($logo)) {
try {
- move_uploaded_file($logo["tmp_name"], ROOT . "/tmp/" . $logo["name"]);
- $logo["tmp_name"] = ROOT . "/tmp/" . $logo["name"];
- if (!isset($logo['name']) || !isset($logo['type']) || !isset($logo['size']) || !isset($logo['tmp_name']) || !is_readable($logo['tmp_name'])) {
- throw new InvalidUploadError($logo, lang('error upload file'));
- } // if
+ if (1) {
+ $x1 = 0 + array_var($_POST, 'x1');
+ $y1 = 0 + array_var($_POST, 'y1');
+ $x2 = 0 + array_var($_POST, 'x2');
+ $y2 = 0 + array_var($_POST, 'y2');
+ $url = $link->getUrl();
+ if (!string_begins_with($url, 'http://')) $url = 'http://' . $url;
+ //die("$x1 $y1 $x2 $y2 $url");
+ $img_data = get_content_from_url('wimg.ca', 80, $url);
+ if ($img_data) {
+ $src_img = imagecreatefromstring($img_data);
+ $dst_img = imagecreatetruecolor(50, 50);
+ imagecopyresized($dst_img, $src_img, 0, 0, $x1, $y1, 50, 50, abs($x2-$x1), abs($y2-$y1) );
+
+ // Output and free from memory
+ //header('Content-Type: image/png');
+ $tempname = tempnam(ROOT . '/tmp/', 'links-snapshot' );
+ imagepng($dst_img, $tempname);
+
+ $logo["name"]='links-snapshot';
+ $logo["tmp_name"]=$tempname;
+ $logo["type"]='image/png';
+ $logo["size"]='1';
+
+ imagedestroy($dst_img);
+ imagedestroy($src_img);
+ }
+ } else {
+ move_uploaded_file($logo["tmp_name"], ROOT . "/tmp/" . $logo["name"]);
+ $logo["tmp_name"] = ROOT . "/tmp/" . $logo["name"];
+ if (!isset($logo['name']) || !isset($logo['type']) || !isset($logo['size']) || !isset($logo['tmp_name']) || !is_readable($logo['tmp_name'])) {
+ throw new InvalidUploadError($logo, lang('error upload file'));
+ } // if
+ }
$valid_types = array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
$max_width = config_option('max_logo_width', 50);
@@ -281,4 +310,4 @@ function delete_logo() {
} // LinksController
-?>
+?>
View
69 application/plugins/links/views/edit_logo.php
@@ -10,41 +10,52 @@
<script type='text/javascript'>
$(function () {
$('#snapshot').imgAreaSelect({
- aspectRatio: '1:1',
- handles: true,
- fadeSpeed: 200,
- resizeable:false,
- maxHeight:300,
- maxWidth:300,
- minHeight:20,
- minWidth:20,
- show: true,
- x1: 0,
- y1: 0,
- x2: 50,
- y2: 50,
- onSelectChange: preview
+ aspectRatio: '1:1',
+ handles: true,
+ fadeSpeed: 200,
+ resizeable:false,
+ maxHeight:300,
+ maxWidth:300,
+ minHeight:20,
+ minWidth:20,
+ show: true,
+ x1: 0,
+ y1: 0,
+ x2: 50,
+ y2: 50,
+ onSelectChange: preview,
+ onSelectEnd: selectend
});
});
function preview(img, selection) {
- if (!selection.width || !selection.height)
- return;
+ if (!selection.width || !selection.height)
+ return;
- //50 is the #preview dimension, change this to your liking
- var scaleX = 50 / selection.width;
- var scaleY = 50 / selection.height;
+ //50 is the #preview dimension, change this to your liking
+ var scaleX = 50 / selection.width;
+ var scaleY = 50 / selection.height;
- $('#preview img').css({
- width: Math.round( scaleX * $('#snapshot').attr('width') ),
- height: Math.round(scaleY * $('#snapshot').attr('height')),
- marginLeft: -Math.round(scaleX * selection.x1),
- marginTop: -Math.round(scaleY * selection.y1)
- });
+ $('#preview img').css({
+ width: Math.round( scaleX * $('#snapshot').attr('width') ),
+ height: Math.round(scaleY * $('#snapshot').attr('height')),
+ marginLeft: -Math.round(scaleX * selection.x1),
+ marginTop: -Math.round(scaleY * selection.y1)
+ });
+ //alert(selection.x);
+}
+function selectend (img, selection) {
+ $('input[name=x1]').val(selection.x1);
+ $('input[name=y1]').val(selection.y1);
+ $('input[name=x2]').val(selection.x2);
+ $('input[name=y2]').val(selection.y2);
}
</script>
<form action="<?php echo $link->getEditLogoUrl() ?>" method="post" enctype="multipart/form-data">
-
+ <input type="hidden" name="x1" value="" />
+ <input type="hidden" name="y1" value="" />
+ <input type="hidden" name="x2" value="" />
+ <input type="hidden" name="y2" value="" />
<?php tpl_display(get_template_path('form_errors')) ?>
<fieldset>
@@ -58,14 +69,14 @@ function preview(img, selection) {
</fieldset>
<fieldset>
<legend><?php echo lang('new logo'); ?></legend>
+<?php if ($link->hasLogo()) { ?>
+ <p class="desc"><?php echo lang('new logo notice') ?></p>
+<?php } // if ?>
<?php echo label_tag(lang('file'), 'linkFormLogo', false) ?>
<?php echo file_field('new_logo', null, array('id' => 'linkFormLogo')) ?>
<?php echo label_tag(lang('snapshot'), 'linkFormSnapshot', false) ?>
<span id="linkFormSnapshot" style="float: left;"><img id="snapshot" src="http://wimg.ca/<?php echo $link->asUrl(); ?>" width="256" height="192"></span>
<span id="preview" style="margin-left: 20px; float: left; display: block; overflow:hidden; width:50px; height:50px;"><img src="http://wimg.ca/<?php echo $link->asUrl(); ?>"></span>
-<?php if ($link->hasLogo()) { ?>
- <p class="desc"><?php echo lang('new logo notice') ?></p>
-<?php } // if ?>
</fieldset>
<?php echo submit_button(lang('upload')) ?> <a href="<?php echo get_url('links'); ?>"><?php echo lang('cancel') ?></a>
</form>
View
76 environment/functions/general.php
@@ -303,6 +303,11 @@ function is_valid_url($url) {
return preg_match(URL_FORMAT, $url);
} // end func is_valid_url
+
+ function ignore_error($errno, $errstr, $errfile, $errline) {
+ return true;
+ }
+
/**
* Redirect to specific URL (header redirection)
*
@@ -324,7 +329,11 @@ function redirect_to($to, $die = true) {
$to = str_replace('&amp;', '&', $to);
} // if
- if(ob_get_level()>0) while(@ob_end_clean());
+ if (ob_get_level()>0) {
+ set_error_handler('ignore_error');
+ while(@ob_end_clean());
+ restore_error_handler();
+ }
header('Location: ' . $to);
if ($die) {
session_write_close();
@@ -510,4 +519,69 @@ function externalUrl($relative_url) {
return $protocol . $_SERVER['HTTP_HOST'] . $relative_url;
}
+ /**
+ * This function returns true if string begins with certain string
+ *
+ * @param string $string
+ * @param string $search
+ * @return boolean
+ */
+ function string_begins_with($string, $search) {
+ return (strncmp($string, $search, strlen($search)) == 0);
+ }
+
+ /**
+ * This function will return chunked content unchunked
+ *
+ * @param string $data
+ * @return $unchunked_data
+ * source: php.net
+ */
+ function http_unchunk($data) {
+ $fp = 0;
+ $outData = '';
+ while ($fp < strlen($data)) {
+ $rawnum = substr($data, $fp, strpos(substr($data, $fp), "\r\n") + 2);
+ $num = hexdec(trim($rawnum));
+ $fp += strlen($rawnum);
+ $chunk = substr($data, $fp, $num);
+ $outData .= $chunk;
+ $fp += strlen($chunk);
+ }
+ return $outData;
+ }
+
+ /**
+ * This function will return content from url
+ * Note: Handles chunked content
+ *
+ * @param string $host
+ * @param string $port
+ * @param string $url (within host)
+ * @return $string with content
+ */
+ function get_content_from_url($host, $port, $url) {
+ $reply='';
+ $fp = fsockopen($host, $port, $errno, $errstr, 30);
+ if (!$fp) {
+ //echo "$errstr ($errno)<br />\n";
+ return false;
+ } else {
+ $out = "GET /$url HTTP/1.0\r\n";
+ $out .= "Host: $host\r\n";
+ $out .= "Connection: Close\r\n\r\n";
+ fwrite($fp, $out);
+ while (!feof($fp)) {
+ $reply .= fgets($fp, 1024);
+ }
+ fclose($fp);
+ }
+ $start_of_data = strpos($reply, "\r\n\r\n")+4;
+ $headers = substr($reply, 0, $start_of_data);
+ $data = substr($reply, $start_of_data);
+ if (strpos(strtolower($headers), "transfer-encoding: chunked") !== FALSE) {
+ $data = http_unchunk($data);
+ }
+ return $data;
+ }
?>
View
4 language/en_us/messages.php
@@ -169,8 +169,8 @@
'error open project' => 'Failed to reopen selected project',
'error edit project logo' => 'Failed to update project logo',
'error delete project logo' => 'Failed to delete project logo',
- 'error edit logo' => 'Failed to update logo',
- 'error delete logo' => 'Failed to delete logo',
+ 'error edit logo' => 'Failed to update logo %s',
+ 'error delete logo' => 'Failed to delete logo %s',
'error delete client' => 'Failed to delete selected client company',
'error delete user' => 'Failed to delete selected user',
'error delete contact' => 'Failed to delete selected contact',
View
71 tools/wimg.php
@@ -0,0 +1,71 @@
+<?php
+//ini_set ('user_agent', $_SERVER['HTTP_USER_AGENT']);
+
+function unchunkHttp11($data) {
+ $fp = 0;
+ $outData = "";
+ while ($fp < strlen($data)) {
+ $rawnum = substr($data, $fp, strpos(substr($data, $fp), "\r\n") + 2);
+ $num = hexdec(trim($rawnum));
+ $fp += strlen($rawnum);
+ $chunk = substr($data, $fp, $num);
+ $outData .= $chunk;
+ $fp += strlen($chunk);
+ }
+ return $outData;
+}
+
+//echo "<xmp>";
+$fp = fsockopen("wimg.ca", 80, $errno, $errstr, 30);
+if (!$fp) {
+ echo "$errstr ($errno)<br />\n";
+} else {
+ $out = "GET /http://www.nu.nl HTTP/1.0\r\n";
+ $out .= "Host: wimg.ca\r\n";
+ $out .= "Connection: Close\r\n\r\n";
+ fwrite($fp, $out);
+ while (!feof($fp)) {
+ $reply .= fgets($fp, 1024);
+ }
+ fclose($fp);
+}
+
+$data = substr($reply, (strpos($reply, "\r\n\r\n")+4));
+if (strpos(strtolower($reply), "transfer-encoding: chunked") !== FALSE) {
+ $data = unchunkHttp11($data);
+}
+
+header('Content-Type: image/png');
+echo $data;
+die();
+
+
+
+
+
+
+// standard routines cannot handle chunked data...
+// the below does not work
+
+
+
+
+$url='http://wimg.ca/http://www.nu.nl';
+$img_with_headers=file_get_contents($url);
+$i=strpos($img_with_headers, "PNG");
+header('Content-Type: image/png');
+echo substr($img_with_headers,$i-1);
+die();
+$i=strpos($img_with_headers, "\r\n\r\n");
+die(substring($img_with_headers,0,$i+7));
+$img = substr($img_with_headers, $i+4);
+$src_img = imagecreatefromstring($img);
+//file_put_contents('wimg.png', $src_img);
+//$dst_img = imagecreatetruecolor(50, 50);
+//imagecopyresized($dst_img, $src_img, 0, 0, $x1, $y1, 50, 50, abs($x2-$x1), abs($y2-$y1) );
+
+// Output and free from memory
+header('Content-Type: image/gif');
+imagegif($src_img);
+//echo $src_img;
+?>

0 comments on commit e437104

Please sign in to comment.
Something went wrong with that request. Please try again.