我正在使用图像扩展来进行图像重新调整大小,但它们不会根据我给出的参数调整大小。这是我的代码。我的代码中有什么错误或什么。调整大小的图像的尺寸等于“800 * 533” 但不完全等于800 * 600。
public function actionCreate()
{
$model=new Business;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Business']))
{
$rnd = rand(0, 9999); // generate random number between 0-9999
$model->attributes = $_POST['Business'];
$uploadedFile = CUploadedFile::getInstance($model, 'image');
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->image = $fileName;
if ($model->save()) {
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
//$uploadedFile->saveAs(Yii::getPathOfAlias('webroot')."/img".$filename);
$uploadedFile->saveAs(Yii::app()->basePath . '/../img/' . $fileName);
$image = Yii::app()->image->load(Yii::app()->basePath . '/../img/' . $fileName);
$image->resize(800, 600);
$image->save(Yii::app()->basePath . '/../img/' . $fileName);
}
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
答案 0 :(得分:0)
首先建议。不要存储您可以使用的未调整大小的图像 CUploadedFile的tempName属性
$image = Yii::app()->image->load($uploadedFile->tempName );
$image->resize(800, 600);
$image->save(Yii::app()->basePath . '/../img/' . $fileName);
关于调整大小我认为你必须计算调整大小的图片的大小。 这是我的代码
protected static function getImgBox($img,$width,$height,$bySide,$boxType){
$img_width=$img->getSize()->getWidth();
$img_height=$img->getSize()->getHeight();
$newWidth =0;
$newHeight=0;
switch($boxType){
case self::BOX_TYPE_FILL:
{
$newWidth=$width;
$newHeight=$height;
}
break;
case self::BOX_TYPE_WO:{
if($bySide==self::BY_SIDE_WIDTH) {
$newWidth = $width;
$newHeight = $img_height * $newWidth / $img_width;
}
if($bySide==self::BY_SIDE_HEIGHT){
$newHeight=$height;
$newWidth = $img_width*$newHeight/$img_height;
}
}
break;
case self::BOX_TYPE_INSIDE:{
$newWidth = $width;
$newHeight = $img_height * $newWidth / $img_width;
if($newHeight>=$height){
$newHeight=$height;
$newWidth = $img_width*$newHeight/$img_height;
}
}
}
if($newHeight!=0 && $newWidth!=0){
return new Box(ceil($newWidth),ceil($newHeight));
}
else
return null;
}
我不知道你使用的女巫延伸。我使用Imagine Extension for Yii 2
答案 1 :(得分:0)
public void InsertProduct(List<string> _myName,
List<int> _myAmount,
List<int> _myPrice, string _date)
{
string connectionString = @"Data Source=MOSTAFA-PC;Initial Catalog=[Sales and Inventory System];Integrated Security=True";
string query = @"INSERT INTO dbo.product(Name, Amount, Price, [date])
VALUES(@Name, @Amount, @Price, @Date);";
using (SqlConnection Con = new SqlConnection(connectionString))
using (SqlCommand Cmd = new SqlCommand(query, Con))
{
Cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Amount", SqlDbType.Int);
Cmd.Parameters.Add("@Price", SqlDbType.Int);
Cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Convert.ToDateTime(_date);
Cmd.Connection = Con;
Con.Open();
int recordsToAdd = _myName.Count();
for(int x = 0; x < recordsToAdd; x++)
{
Cmd.Parameters["@Name"].Value = _myName[x];
Cmd.Parameters["@Amount"].Value = _myAmount[x];
Cmd.Parameters["@Price"].Value = _myPrice[x];
Cmd.ExecuteNonQuery();
}
}