PHP captha image not working - captcha

I have tried this code to create a captcha image
<?PHP
header('Content-type: image/jpeg');
$size=25;
$captha_width=100;
$captha_height=40;
$text=rand(10000,99999);
$captha=imagecreate($captha_width,$captha_height);
imagecolorallocate($captha,192,192,192);
$color=imagecolorallocate($captha,10,10,10);
for($i=1;$i<30;++$i){
$a1=rand(0,100);
$a2=rand(0,100);
$a3=rand(0,100);
$a4=rand(0,100);
imageline($captha,$a1,$a2,$a3,$a4,$color);
}
imagettftext($captha,$size,0,15,30,$color,'face.ttf',$text);
imagejpeg($captha);
?>
But when I open it in my browser it shows only corrupted image icon why???

Related

Install ckeditor in YII framework

I am using Yii Framework version 1.1.14. I am able to install FCK editor but I want to use ck editor.
I download files from this links http://www.yiiframework.com/extension/the-ckeditor-integration/
and upload files on location but i get this error
check image
I am using this code in my view
<?php $this->widget('application.extensions.ckeditor.CKEditorWidget',array(
'model'=>$model, # Data-Model (form model)
'attribute'=>'content', # Attribute in the Data-Model
'height'=>'400px',
'width'=>'100%',
'toolbarSet'=>'Basic', # EXISTING(!) Toolbar (see: ckeditor.js)
'ckeditor'=>Yii::app()->basePath.'/../ckeditor/ckeditor.php',
# Path to ckeditor.php
'ckBasePath'=>Yii::app()->baseUrl.'/ckeditor/',
# Relative Path to the Editor (from Web-Root)
'css' => Yii::app()->baseUrl.'/css/index.css',
# Additional Parameters
) ); ?>
Download CKEditor from http://ckeditor.com/download (Select full package) and put it in your root directory (You can put it any where according to you)
Add this is your view file
<script src="<?php echo Yii::app()->baseUrl.'/ckeditor/ckeditor.js'; ?>"></script>
<script type="text/javascript">
CKEDITOR.replace( 'Articles_meta_description');
</script>
Where Articles_meta_description is id of input fields
If you want add image upload in ckeditor you can download KCfinder from this link http://kcfinder.sunhater.com/download and put it in your root directory (you can put it any where according to you)
Set upload path into session : in your view file
$_SESSION['KCFINDER']['disabled'] = false; // enables the file browser in the admin
$_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/uploads/"; // URL for the uploads folder
$_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../uploads/"; //
For ckeditor with image upload
<script type="text/javascript">
CKEDITOR.replace( 'Articles_meta_description', { // input field id
filebrowserBrowseUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/browse.php?type=files',
filebrowserImageBrowseUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/browse.php?type=images',
filebrowserFlashBrowseUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/browse.php?type=flash',
filebrowserUploadUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/upload.php?type=files',
filebrowserImageUploadUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/upload.php?type=images',
filebrowserFlashUploadUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/upload.php?type=flash'
});
</script>

Opencart - Upload Specific File Type

I am using OpenCart 1.5.6 and have two options for the customer to upload files to a product (image and video). When the users clicks upload I would like them to only be able to select specific file types from their computer while they browse for the files. So instead of "All Files" in the browser window, I want it to only allow .png, .jpg, .jpeg, .pdf, etc. for the image upload and for the video file I just want it to look for .mp4, .mov, .flv.
Here is an example: http://jsfiddle.net/VCdvA/
I have tried to use the accept attribute according to this post but it doesn't work.
Here is my code for the product.tpl:
<input type="button" accept="video/*,image/*" value="<?php echo $button_upload; ?>" id="button-option-<?php echo $option['product_option_id']; ?>" class="button" >
<input type="hidden" name="option[<?php echo $option['product_option_id']; ?>]" value="" />
I think that the problem is that it is type="button" and not type="file". Is there another way to limit the upload file type within the OpenCart product page?
The problem is the jQuery plugin used. It is AjaxUpload which seems to be taken over by different developer (completely) and reworked (completely) and the links to the original (old) documentation are dead (completely).
Well, after exploring that jQuery plugin I have found out that it works this way: in the ptoduct.tpl You can see:
<?php foreach ($options as $option) { ?>
<?php if ($option['type'] == 'file') { ?>
<script type="text/javascript"><!--
new AjaxUpload('#button-option-<?php echo $option['product_option_id']; ?>', {
//...
});
//--></script>
<?php } ?>
<?php } ?>
This means that for each option of type file this AjaxUpload is instanciated. Basically it only creates a DIV containing INPUT of type FILE with 0 opacity that is rendered above the Upload File button. This means that when user clicks on Upload File he clicks on the INPUT of type FILE instead (without knowing this). Now, looking at the code I can see that there is no possibility to set this accept="image/*" attribute because there is no support for this (original code):
_createInput: function(){
var self = this;
var input = document.createElement("input");
input.setAttribute('type', 'file');
input.setAttribute('name', this._settings.name);
addStyles(input, {
// ...
});
// ...
},
So now if You'd like to add support for this, You'd need to change this code to this (file: catalog/view/javascript/jquery/ajaxupload.js, around line 350):
var input = document.createElement("input");
input.setAttribute('type', 'file');
input.setAttribute('name', this._settings.name);
if (this._settings.accept) {
input.setAttribute('accept', this._settings.accept);
}
And change the code for plugin initializiation in product.tpl to:
<?php foreach ($options as $option) { ?>
<?php if ($option['type'] == 'file') { ?>
<script type="text/javascript"><!--
new AjaxUpload('#button-option-<?php echo $option['product_option_id']; ?>', {
<?php if ($option['product_option_id'] == <IMAGE_FILE_OPTION_ID>) { ?>
accept: 'image/*',
<?php } else if ($option['product_option_id'] == <VIDEO_FILE_OPTION_ID>) { ?>
accept: 'video/*',
<?php } ?>
//...
});
//--></script>
<?php } ?>
<?php } ?>
This should be enough.

Changing text beside upload field in Yii Framework

hopefully a quick question here, have not been able to find out the answer on google. Anyway I have the following File upload field.
<div class="row">
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?>
<?php echo $form->labelEx($model, 'doc'); ?>
<?php echo CHtml::activeFileField($model, 'doc'); ?>
<?php echo $form->error($model, 'doc'); ?>
</div>
The question I have is how to change the default text? On the page in question the upload field comes up with "No file chosen" beside the button when the page loads. I wish to change this text, anyone tell me how? Thanks.
I quick & dirty fix is this:
<?php echo CHtml::activeFileField($model, 'type',array('style'=>'width:100px;', 'onchange' => 'this.style.width = "100%";')); ?>

Why imagecreatefromjpeg does not work in yii framework?

I'm trying to resize an image at runtime in yii, but does not work.
I tried this code in the view but it does not work.
My code in view.php
header('Content-Type: image/jpeg');
$image = imagecreatefromjpeg('image.jpg');
echo imagejpeg($image);
My code in controller
public function actionImage()
{
$this->render('image');
}
Output html
<html debug="true">
<body style="margin: 0px; ">
<img style="-webkit-user-select: none; " src="http://localhost/yiiadministration/index.php?r=administration/products/image"/>
</body>
<script src="chrome-extension://bmagokdooijbeehmkpknfglimnifench/googleChrome.js"/>
</html>
P.S; the code in the view is just to see the operation, and not to scale the image, because I normally do this in php.
Can anyone help?
A header has already been sent before the view is rendered so your code tries to send a new header and fails as it has already been sent.
You can either send that header from the controller or use an img tag in the view as said previously on stack overflow here
Also imagejpeg() outputs the image directly to the browser so the echo is wrong in that context.

show uploaded image yii

I am new to php and yii. I have a file field in my form to upload images. I use the following code to upload the image using file field.
Code in View:
<?php echo $form->fileField($model,'logo', array('class'=>'input-file')); ?>
<img src="<?php
echo Yii::app()->request->baseUrl.'/protected/uploads/sitelogo/'.$savedvalues['varLogo'];
?>" width="50" height="50" />
<?php echo $form->hiddenField($model,'hiddenfile',
array('value'=>$savedvalues['varLogo'])); ?>
code in controller:
$randnum = rand(0,100);
$home->varLogo = $randnum.$model->logo;
$file= Yii::app()->getBasePath().'/uploads/sitelogo/'.$randnum.$model->logo;
$model->logo->saveAs($file);
The images are uploading fine now. I have saved the uploaded images in protected\uploads\ folder. I am trying to show the uploaded image in edit image section. But the image doesn't display. It Shows failed to load the given url in firebug.
How can i correct this issue?
protected folder is protected for some reason - nobody can access it
if you want to access your pictures move them to public folder (or public/uploaded)
here is .htaccess content for protected
deny from all
EDIT:
of course you can put your files into protected folder(or make uploaded folder not accessible too) if you want some ACL for your users, and then check access, read and output file by your script(class)
in view:
<img src="<?php
echo Yii::app()->request->baseUrl.'/image.php?url=protected/uploads/sitelogo/'.$savedvalues['varLogo'];
?>" width="50" height="50" />
in your controller or class:
if (Yii::app()->session['user_can_access_files']) {
header('Content-Type: image/jpeg');
readfile($_GET['url']);
} else {
Yii::app()->user->loginRequired();
}
Caveat: I'm a total yii noob, so take this w/ a grain of salt :)
I took a similar approach, but have user-specific folders & added a MIME type check.
I upload files in a similar way, then view them with this in the view:
echo '<embed src="/tim_0.5/index.php/files/Getdatafile/' . $modelid . '" width=100% height=400>';
And from my controller I have:
public function actionGetdatafile($id)
{
$model=$this->loadModel($id);
$imgpath = $model->user_id . '/' . $model->filename;
$file=Yii::app()->getBasePath().'/datafiles/'.$imgpath;
if (file_exists($file))
{
$img=getimagesize($file);
header('Content-Type: '.$img['mime']);
readfile($file);
exit;
} else {
echo 'File not found!';
}
}
<b><?php echo CHtml::encode($data->getAttributeLabel('binaryfile')); ?>:</b>
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/upload/'.$data->fileName); ?>
<br />
filename is name of your file which you have stored in upload folder in root directory.
(Yii::app()->request->baseUrl.'/upload/' point to your upload folder which is in your root directory
Hope it works....